summarylogtreecommitdiffstats
path: root/0001-ROG-ALLY-bmi323-device.patch
blob: e83d6e3e18300191021747a59a194f14f3a1901a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
From 622ea77bfccd751247b1c08c3126d7ab716f0423 Mon Sep 17 00:00:00 2001
From: Denis <benato.denis96@gmail.com>
Date: Mon, 25 Sep 2023 03:38:49 +0200
Subject: [PATCH] This commit adds support to the bmi323 device on top of the
 pre-existing bmc150 kernel module.

Some new devices for example the ROG Ally and the Air Plus identify this chip in the ACPI table as a bmc150 so previously the original module was loaded,
but was erroring out as it cannot handle such device.

The device I own does not allow me to use the interrupt part of the device as the interrupt pin is not connected (or not advertised to be connected) hence
I avoided including on this commit anything related to IRQ.

This driver has already been proved to work well enough to be used in the switch emulator "yuzu".

While designing this module my main focus was not to alter the original driver and not to limit the original author in regard to future mofications,
and I was mostly able to achive this, except:
1) I added a new structure on top of the original one and added a field that is responsible for holding information
on what type of chip the module is currently managing
2) the previous point required the init function of the original driver to write that field in order to be sure no bmi323 code
was executed when the old part of the module is managing the device
3) as the original driver issued an i2c write on some register not really meant to be written in the bmi323 device I have made sure an i2c read to discover
the bmi323 is performed prior to that code: such read SHOULD fail in the older bmc150 IC for two reasons:
    - the i2c address is not reported in the memory map of the bmc150 in its datasheet
    - the i2c read attempts to get 4 bytes out of a 8-bit device
    - the fourth bit (the one that cannot be read from a bmc150 device) is initialized to 0 and bmi323 presence is signaled with a 1 in the LSB
      that is the fourth coming out of the device in temporal order
---
 drivers/iio/accel/bmc150-accel-core.c | 2307 ++++++++++++++++++++++++-
 drivers/iio/accel/bmc150-accel-i2c.c  |  100 +-
 drivers/iio/accel/bmc150-accel.h      |   94 +-
 3 files changed, 2495 insertions(+), 6 deletions(-)

diff --git a/drivers/iio/accel/bmc150-accel-core.c b/drivers/iio/accel/bmc150-accel-core.c
index 110591804b4c..9a2c1732c9ef 100644
--- a/drivers/iio/accel/bmc150-accel-core.c
+++ b/drivers/iio/accel/bmc150-accel-core.c
@@ -130,6 +130,73 @@
 #define BMC150_ACCEL_REG_FIFO_DATA		0x3F
 #define BMC150_ACCEL_FIFO_LENGTH		32
 
+#define BMC150_BMI323_TEMPER_CENTER_VAL 23
+#define BMC150_BMI323_TEMPER_LSB_PER_KELVIN_VAL 512
+
+#define BMC150_BMI323_AUTO_SUSPEND_DELAY_MS 2000
+
+#define BMC150_BMI323_CHIP_ID_REG 0x00
+#define BMC150_BMI323_SOFT_RESET_REG 0x7E
+#define BMC150_BMI323_SOFT_RESET_VAL 0xDEAFU
+#define BMC150_BMI323_DATA_BASE_REG 0x03
+#define BMC150_BMI323_TEMPERATURE_DATA_REG 0x09
+#define BMC150_BMI323_FIFO_FILL_LEVEL_REG 0x15
+#define BMC150_BMI323_FIFO_DATA_REG 0x16
+#define BMC150_BMI323_ACC_CONF_REG 0x20
+#define BMC150_BMI323_GYR_CONF_REG 0x21
+#define BMC150_BMI323_FIFO_CONF_REG 0x36
+
+// these are bits [0:3] of ACC_CONF.acc_odr, sample rate in Hz for the accel part of the chip
+#define BMC150_BMI323_ACCEL_ODR_0_78123_VAL 0x0001
+#define BMC150_BMI323_ACCEL_ODR_1_5625_VAL 0x0002
+#define BMC150_BMI323_ACCEL_ODR_3_125_VAL 0x0003
+#define BMC150_BMI323_ACCEL_ODR_6_25_VAL 0x0004
+#define BMC150_BMI323_ACCEL_ODR_12_5_VAL 0x0005
+#define BMC150_BMI323_ACCEL_ODR_25_VAL 0x0006
+#define BMC150_BMI323_ACCEL_ODR_50_VAL 0x0007
+#define BMC150_BMI323_ACCEL_ODR_100_VAL 0x0008
+#define BMC150_BMI323_ACCEL_ODR_200_VAL 0x0009
+#define BMC150_BMI323_ACCEL_ODR_400_VAL 0x000A
+#define BMC150_BMI323_ACCEL_ODR_800_VAL 0x000B
+#define BMC150_BMI323_ACCEL_ODR_1600_VAL 0x000C
+#define BMC150_BMI323_ACCEL_ODR_3200_VAL 0x000D
+#define BMC150_BMI323_ACCEL_ODR_6400_VAL 0x000E
+
+#define BMC150_BMI323_ACCEL_BW_ODR_2_VAL 0x0000
+#define BMC150_BMI323_ACCEL_BW_ODR_4_VAL 0x0001
+
+// these are bits [4:6] of ACC_CONF.acc_range, full scale resolution
+#define BMC150_BMI323_ACCEL_RANGE_2_VAL 0x0000 // +/-2g, 16.38 LSB/mg
+#define BMC150_BMI323_ACCEL_RANGE_4_VAL 0x0001 // +/-4g, 8.19 LSB/mg
+#define BMC150_BMI323_ACCEL_RANGE_8_VAL 0x0002 // +/-8g, 4.10 LSB/mg
+#define BMC150_BMI323_ACCEL_RANGE_16_VAL 0x0003 // +/-4g, 2.05 LSB/mg
+
+// these are bits [0:3] of GYR_CONF.gyr_odr, sample rate in Hz for the gyro part of the chip
+#define BMC150_BMI323_GYRO_ODR_0_78123_VAL 0x0001
+#define BMC150_BMI323_GYRO_ODR_1_5625_VAL 0x0002
+#define BMC150_BMI323_GYRO_ODR_3_125_VAL 0x0003
+#define BMC150_BMI323_GYRO_ODR_6_25_VAL 0x0004
+#define BMC150_BMI323_GYRO_ODR_12_5_VAL 0x0005
+#define BMC150_BMI323_GYRO_ODR_25_VAL 0x0006
+#define BMC150_BMI323_GYRO_ODR_50_VAL 0x0007
+#define BMC150_BMI323_GYRO_ODR_100_VAL 0x0008
+#define BMC150_BMI323_GYRO_ODR_200_VAL 0x0009
+#define BMC150_BMI323_GYRO_ODR_400_VAL 0x000A
+#define BMC150_BMI323_GYRO_ODR_800_VAL 0x000B
+#define BMC150_BMI323_GYRO_ODR_1600_VAL 0x000C
+#define BMC150_BMI323_GYRO_ODR_3200_VAL 0x000D
+#define BMC150_BMI323_GYRO_ODR_6400_VAL 0x000E
+
+#define BMC150_BMI323_GYRO_BW_ODR_2_VAL 0x0000
+#define BMC150_BMI323_GYRO_BW_ODR_4_VAL 0x0001
+
+// these are bits [4:6] of GYR_CONF.gyr_range, full scale resolution
+#define BMC150_BMI323_GYRO_RANGE_125_VAL 0x0000 // +/-125°/s, 262.144 LSB/°/s
+#define BMC150_BMI323_GYRO_RANGE_250_VAL 0x0001 // +/-250°/s,  131.2 LSB/°/s
+#define BMC150_BMI323_GYRO_RANGE_500_VAL 0x0002 // +/-500°/s,  65.6 LSB/°/s
+#define BMC150_BMI323_GYRO_RANGE_1000_VAL 0x0003 // +/-1000°/s, 32.8 LSB/°/s
+#define BMC150_BMI323_GYRO_RANGE_2000_VAL 0x0004 // +/-2000°/s, 16.4 LSB/°/s
+
 enum bmc150_accel_axis {
 	AXIS_X,
 	AXIS_Y,
@@ -149,6 +216,654 @@ struct bmc150_scale_info {
 	u8 reg_range;
 };
 
+/*
+ * This enum MUST not be altered as there are parts in the code that
+ * uses an int conversion to get the correct device register to read.
+ */
+enum bmi323_axis {
+	BMI323_ACCEL_AXIS_X = 0,
+	BMI323_ACCEL_AXIS_Y,
+	BMI323_ACCEL_AXIS_Z,
+	BMI323_GYRO_AXIS_X,
+	BMI323_GYRO_AXIS_Y,
+	BMI323_GYRO_AXIS_Z,
+	BMI323_TEMP,
+	BMI323_AXIS_MAX,
+};
+
+static const struct bmi323_scale_accel_info {
+	u8 hw_val;
+	int val;
+	int val2;
+	int ret_type;
+} bmi323_accel_scale_map[] = {
+	{
+		.hw_val = (u16)BMC150_BMI323_ACCEL_RANGE_2_VAL << (u16)4,
+		.val = 0,
+		.val2 = 598,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.hw_val = (u16)BMC150_BMI323_ACCEL_RANGE_4_VAL << (u16)4,
+		.val = 0,
+		.val2 = 1196,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.hw_val = (u16)BMC150_BMI323_ACCEL_RANGE_8_VAL << (u16)4,
+		.val = 0,
+		.val2 = 2392,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.hw_val = (u16)BMC150_BMI323_ACCEL_RANGE_16_VAL << (u16)4,
+		.val = 0,
+		.val2 = 4785,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+};
+
+static const struct bmi323_scale_gyro_info {
+	u8 hw_val;
+	int val;
+	int val2;
+	int ret_type;
+} bmi323_gyro_scale_map[] = {
+	{
+		.hw_val = (u16)BMC150_BMI323_GYRO_RANGE_125_VAL << (u16)4,
+		.val = 0,
+		.val2 = 66545,
+		.ret_type = IIO_VAL_INT_PLUS_NANO,
+	},
+	{
+		.hw_val = (u16)BMC150_BMI323_GYRO_RANGE_125_VAL << (u16)4,
+		.val = 0,
+		.val2 = 66,
+		.ret_type = IIO_VAL_INT_PLUS_NANO,
+	},
+	{
+		.hw_val = (u16)BMC150_BMI323_GYRO_RANGE_250_VAL << (u16)4,
+		.val = 0,
+		.val2 = 133090,
+		.ret_type = IIO_VAL_INT_PLUS_NANO,
+	},
+	{
+		.hw_val = (u16)BMC150_BMI323_GYRO_RANGE_250_VAL << (u16)4,
+		.val = 0,
+		.val2 = 133,
+		.ret_type = IIO_VAL_INT_PLUS_NANO,
+	},
+	{
+		.hw_val = (u16)BMC150_BMI323_GYRO_RANGE_500_VAL << (u16)4,
+		.val = 0,
+		.val2 = 266181,
+		.ret_type = IIO_VAL_INT_PLUS_NANO,
+	},
+	{
+		.hw_val = (u16)BMC150_BMI323_GYRO_RANGE_500_VAL << (u16)4,
+		.val = 0,
+		.val2 = 266,
+		.ret_type = IIO_VAL_INT_PLUS_NANO,
+	},
+	{
+		.hw_val = (u16)BMC150_BMI323_GYRO_RANGE_1000_VAL << (u16)4,
+		.val = 0,
+		.val2 = 532362,
+		.ret_type = IIO_VAL_INT_PLUS_NANO,
+	},
+	{
+		.hw_val = (u16)BMC150_BMI323_GYRO_RANGE_1000_VAL << (u16)4,
+		.val = 0,
+		.val2 = 532,
+		.ret_type = IIO_VAL_INT_PLUS_NANO,
+	},
+	{
+		.hw_val = (u16)BMC150_BMI323_GYRO_RANGE_2000_VAL << (u16)4,
+		.val = 0,
+		.val2 = 1064724,
+		.ret_type = IIO_VAL_INT_PLUS_NANO,
+	},
+	{
+		// this shouldn't be necessary, but iio seems to have a wrong rounding of this value...
+		.hw_val = (u16)BMC150_BMI323_GYRO_RANGE_2000_VAL << (u16)4,
+		.val = 0,
+		.val2 = 1064,
+		.ret_type = IIO_VAL_INT_PLUS_NANO,
+	},
+	{
+		.hw_val = (u16)BMC150_BMI323_GYRO_RANGE_2000_VAL << (u16)4,
+		.val = 0,
+		.val2 = 1065,
+		.ret_type = IIO_VAL_INT_PLUS_NANO,
+	},
+};
+
+/*
+ * this reflects the frequency map that is following.
+ * For each index i of that map index i*2 and i*2+1 of of this
+ * holds ODR/2 and ODR/4
+ */
+static const struct bmi323_3db_freq_cutoff_accel_info {
+	int val;
+	int val2;
+	int ret_type;
+} bmi323_accel_3db_freq_cutoff[] = {
+	{
+		.val = 0,
+		.val2 = 390615,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 0,
+		.val2 = 195308,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 0,
+		.val2 = 781300,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 0,
+		.val2 = 390650,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 1,
+		.val2 = 562500,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 0,
+		.val2 = 78125,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 3,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 1,
+		.val2 = 500000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 6,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 3,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 12,
+		.val2 = 500000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 6,
+		.val2 = 250000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 25,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 12,
+		.val2 = 500000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 50,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 25,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 100,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 50,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 200,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 100,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 400,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 200,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 800,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 400,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 1600,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 800,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 1600,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 800,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 3200,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 1600,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+};
+
+static const struct bmi323_freq_accel_info {
+	u8 hw_val;
+	int val;
+	int val2;
+	s64 time_ns;
+} bmi323_accel_odr_map[] = {
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_0_78123_VAL,
+		.val = 0,
+		.val2 = 781230,
+		.time_ns = 1280032769,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_1_5625_VAL,
+		.val = 1,
+		.val2 = 562600,
+		.time_ns = 886522247,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_3_125_VAL,
+		.val = 3,
+		.val2 = 125000,
+		.time_ns = 320000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_6_25_VAL,
+		.val = 6,
+		.val2 = 250000,
+		.time_ns = 160000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_12_5_VAL,
+		.val = 12,
+		.val2 = 500000,
+		.time_ns = 80000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_25_VAL,
+		.val = 25,
+		.val2 = 0,
+		.time_ns = 40000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_50_VAL,
+		.val = 50,
+		.val2 = 0,
+		.time_ns = 20000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_100_VAL,
+		.val = 100,
+		.val2 = 0,
+		.time_ns = 10000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_200_VAL,
+		.val = 200,
+		.val2 = 0,
+		.time_ns = 5000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_400_VAL,
+		.val = 400,
+		.val2 = 0,
+		.time_ns = 2500000,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_800_VAL,
+		.val = 800,
+		.val2 = 0,
+		.time_ns = 1250000,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_1600_VAL,
+		.val = 1600,
+		.val2 = 0,
+		.time_ns = 625000,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_3200_VAL,
+		.val = 3200,
+		.val2 = 0,
+		.time_ns = 312500,
+	},
+	{
+		.hw_val = BMC150_BMI323_ACCEL_ODR_6400_VAL,
+		.val = 6400,
+		.val2 = 0,
+		.time_ns = 156250,
+	},
+};
+
+static const struct bmi323_freq_gyro_info {
+	u8 hw_val;
+	int val;
+	int val2;
+	s64 time_ns;
+} bmi323_gyro_odr_map[] = {
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_0_78123_VAL,
+		.val = 0,
+		.val2 = 781230,
+		.time_ns = 1280032769,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_1_5625_VAL,
+		.val = 1,
+		.val2 = 562600,
+		.time_ns = 886522247,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_3_125_VAL,
+		.val = 3,
+		.val2 = 125000,
+		.time_ns = 320000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_6_25_VAL,
+		.val = 6,
+		.val2 = 250000,
+		.time_ns = 160000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_12_5_VAL,
+		.val = 12,
+		.val2 = 500000,
+		.time_ns = 80000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_25_VAL,
+		.val = 25,
+		.val2 = 0,
+		.time_ns = 40000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_50_VAL,
+		.val = 50,
+		.val2 = 0,
+		.time_ns = 20000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_100_VAL,
+		.val = 100,
+		.val2 = 0,
+		.time_ns = 10000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_200_VAL,
+		.val = 200,
+		.val2 = 0,
+		.time_ns = 5000000,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_400_VAL,
+		.val = 400,
+		.val2 = 0,
+		.time_ns = 2500000,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_800_VAL,
+		.val = 800,
+		.val2 = 0,
+		.time_ns = 1250000,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_1600_VAL,
+		.val = 1600,
+		.val2 = 0,
+		.time_ns = 625000,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_3200_VAL,
+		.val = 3200,
+		.val2 = 0,
+		.time_ns = 312500,
+	},
+	{
+		.hw_val = BMC150_BMI323_GYRO_ODR_6400_VAL,
+		.val = 6400,
+		.val2 = 0,
+		.time_ns = 156250,
+	},
+};
+
+static const struct bmi323_3db_freq_cutoff_gyro_info {
+	int val;
+	int val2;
+	int ret_type;
+} bmi323_gyro_3db_freq_cutoff[] = {
+	{
+		.val = 0,
+		.val2 = 390615,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 0,
+		.val2 = 1953075, // TODO: check if this gets reported correctly...
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 0,
+		.val2 = 781300,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 0,
+		.val2 = 390650,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 1,
+		.val2 = 562500,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 0,
+		.val2 = 78125,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 3,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 1,
+		.val2 = 500000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 6,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 3,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 12,
+		.val2 = 500000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 6,
+		.val2 = 250000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 25,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 12,
+		.val2 = 500000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 50,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 25,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 100,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 50,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 200,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 100,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 400,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 200,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 800,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 400,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 1600,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 800,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 1600,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 800,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 3200,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+	{
+		.val = 1600,
+		.val2 = 000000,
+		.ret_type = IIO_VAL_INT_PLUS_MICRO,
+	},
+};
+
+static const int bmi323_accel_scales[] = {
+	0, 598, 0, 1196, 0, 2392, 0, 4785,
+};
+
+static const int bmi323_gyro_scales[] = {
+	0, 66545, 0, 133090, 0, 266181, 0, 532362, 0, 1064724,
+};
+
+static const int bmi323_sample_freqs[] = {
+	0,   781230, 1,	   562600, 3,	 125000, 6,    250000, 12,  500000,
+	25,  0,	     50,   0,	   100,	 0,	 200,  0,      400, 0,
+	800, 0,	     1600, 0,	   3200, 0,	 6400, 0,
+};
+
+static const struct {
+	int val;
+	int val2; // IIO_VAL_INT_PLUS_MICRO
+	u8 bw_bits;
+} bmi323_samp_freq_table[] = { { 15, 620000, 0x08 }, { 31, 260000, 0x09 },
+			       { 62, 500000, 0x0A }, { 125, 0, 0x0B },
+			       { 250, 0, 0x0C },     { 500, 0, 0x0D },
+			       { 1000, 0, 0x0E },    { 2000, 0, 0x0F } };
+
 struct bmc150_accel_chip_info {
 	const char *name;
 	u8 chip_id;
@@ -1113,6 +1828,52 @@ static const struct iio_event_spec bmc150_accel_event = {
 	.num_event_specs = 1						\
 }
 
+#define BMI323_ACCEL_CHANNEL(_axis, bits)                                      \
+	{                                                                      \
+		.type = IIO_ACCEL, .modified = 1, .channel2 = IIO_MOD_##_axis, \
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                  \
+		.info_mask_shared_by_type =                                    \
+			BIT(IIO_CHAN_INFO_SCALE) |                             \
+			BIT(IIO_CHAN_INFO_SAMP_FREQ) |                         \
+			BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),      \
+		.info_mask_shared_by_type_available =                          \
+			BIT(IIO_CHAN_INFO_SAMP_FREQ) |                         \
+			BIT(IIO_CHAN_INFO_SCALE),                              \
+		.scan_index = BMI323_ACCEL_AXIS_##_axis,                       \
+		.scan_type = {                                                 \
+			.sign = 's',                                           \
+			.realbits = (bits),                                    \
+			.storagebits = 16,                                     \
+			.shift = 16 - (bits),                                  \
+			.endianness = IIO_LE,                                  \
+		},                                                             \
+	}
+
+#define BMI323_GYRO_CHANNEL(_axis, bits)                                  \
+	{                                                                 \
+		.type = IIO_ANGL_VEL, .modified = 1,                      \
+		.channel2 = IIO_MOD_##_axis,                              \
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),             \
+		.info_mask_shared_by_type =                               \
+			BIT(IIO_CHAN_INFO_SCALE) |                        \
+			BIT(IIO_CHAN_INFO_SAMP_FREQ) |                    \
+			BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
+		.info_mask_shared_by_type_available =                     \
+			BIT(IIO_CHAN_INFO_SAMP_FREQ) |                    \
+			BIT(IIO_CHAN_INFO_SCALE),                         \
+		.scan_index = BMI323_GYRO_AXIS_##_axis,                   \
+		.scan_type = {                                            \
+			.sign = 's',                                      \
+			.realbits = (bits),                               \
+			.storagebits = 16,                                \
+			.shift = 16 - (bits),                             \
+			.endianness = IIO_LE,                             \
+		},                                                        \
+		/*.ext_info = bmi323_accel_ext_info,*/                    \
+		/*.event_spec = &bmi323_accel_event,*/                    \
+		/*.num_event_specs = 1*/                                  \
+	}
+
 #define BMC150_ACCEL_CHANNELS(bits) {					\
 	{								\
 		.type = IIO_TEMP,					\
@@ -1595,7 +2356,7 @@ static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
 	struct device *dev = regmap_get_device(data->regmap);
 	int ret, i;
 	unsigned int val;
-
+	
 	/*
 	 * Reset chip to get it in a known good state. A delay of 1.8ms after
 	 * reset is required according to the data sheets of supported chips.
@@ -1677,6 +2438,11 @@ int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
 	data = iio_priv(indio_dev);
 	dev_set_drvdata(dev, indio_dev);
 
+	/*
+	 * Setting the dev_type here is necessary to avoid having it left uninitialized
+	 * and therefore potentially executing bmi323 functions for the original bmc150 model.
+	 */
+	data->dev_type = BMC150;
 	data->regmap = regmap;
 	data->type = type;
 
@@ -1826,12 +2592,1407 @@ void bmc150_accel_core_remove(struct device *dev)
 }
 EXPORT_SYMBOL_NS_GPL(bmc150_accel_core_remove, IIO_BMC150);
 
-#ifdef CONFIG_PM_SLEEP
-static int bmc150_accel_suspend(struct device *dev)
+struct device *bmi323_get_managed_device(struct bmi323_private_data *bmi323)
+{
+	if (bmi323->i2c_client != NULL)
+		return &bmi323->i2c_client->dev;
+
+	return &bmi323->spi_client->dev;
+}
+
+static int bmi323_set_power_state(struct bmi323_private_data *bmi323, bool on)
+{
+#ifdef CONFIG_PM
+	struct device *dev = bmi323_get_managed_device(bmi323);
+	int ret;
+
+	if (on)
+		ret = pm_runtime_get_sync(dev);
+	else {
+		pm_runtime_mark_last_busy(dev);
+		ret = pm_runtime_put_autosuspend(dev);
+	}
+
+	if (ret < 0) {
+		dev_err(dev, "bmi323_set_power_state failed with %d\n", on);
+
+		if (on)
+			pm_runtime_put_noidle(dev);
+
+		return ret;
+	}
+#endif
+
+	return 0;
+}
+
+int bmi323_write_u16(struct bmi323_private_data *bmi323, u8 in_reg,
+		     u16 in_value)
+{
+	s32 ret;
+
+	if (bmi323->i2c_client != NULL) {
+		ret = i2c_smbus_write_i2c_block_data(bmi323->i2c_client, in_reg,
+						     sizeof(in_value),
+						     (u8 *)(&in_value));
+		if (ret != 0) {
+			return -2;
+		}
+
+		return 0;
+	} else if (bmi323->spi_client != NULL) {
+		/*
+		 * To whoever may need this: implementing this should be straightforward:
+		 * it's specular to the i2c part.
+		 */
+
+		return -EINVAL; // TODO: change with 0 once implemented
+	}
+
+	return -EINVAL;
+}
+EXPORT_SYMBOL_NS_GPL(bmi323_write_u16, IIO_BMC150);
+
+int bmi323_read_u16(struct bmi323_private_data *bmi323, u8 in_reg,
+		    u16 *out_value)
+{
+	s32 ret;
+	u8 read_bytes[4];
+
+	if (bmi323->i2c_client != NULL) {
+		ret = i2c_smbus_read_i2c_block_data(bmi323->i2c_client, in_reg,
+						    sizeof(read_bytes),
+						    &read_bytes[0]);
+		if (ret != 4) {
+			return ret;
+		}
+
+		// DUMMY	= read_bytes[0]
+		// DUMMY	= read_bytes[1]
+		// LSB		= read_bytes[2]
+		// MSB		= read_bytes[3]
+		u8 *o = (u8 *)out_value;
+		o[0] = read_bytes[2];
+		o[1] = read_bytes[3];
+
+		return 0;
+	} else if (bmi323->spi_client != NULL) {
+		printk(KERN_CRIT
+		       "bmi323: SPI interface is not yet implemented.\n");
+
+		/*
+		 * To whoever may need this: implementing this should be straightforward:
+		 * it's specular to the i2c part except that the dummy data is just 1 byte.
+		 */
+
+		return -EINVAL; // TODO: change with 0 once implemented
+	}
+
+	return -EINVAL;
+}
+EXPORT_SYMBOL_NS_GPL(bmi323_read_u16, IIO_BMC150);
+
+int bmi323_chip_check(struct bmi323_private_data *bmi323)
+{
+	u16 chip_id;
+	int ret;
+
+	ret = bmi323_read_u16(bmi323, BMC150_BMI323_CHIP_ID_REG, &chip_id);
+	if (ret != 0) {
+		return ret;
+	}
+
+	if (((chip_id)&0x00FF) != cpu_to_le16((u16)0x0043U)) {
+		dev_err(bmi323->dev,
+			"bmi323_chip_check failed with: %d; chip_id = 0x%04x",
+			ret, chip_id);
+
+		return -EINVAL;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(bmi323_chip_check, IIO_BMC150);
+
+static int bmi323_buffer_preenable(struct iio_dev *indio_dev)
+{
+	struct bmc150_accel_data *data = iio_priv(indio_dev);
+
+	const int ret = bmi323_set_power_state(&data->bmi323, true);
+
+	if (ret == 0) {
+		mutex_lock(&data->bmi323.mutex);
+		data->bmi323.fifo_frame_time_diff_ns =
+			(data->bmi323.acc_odr_time_ns >=
+			 data->bmi323.gyr_odr_time_ns) ?
+				data->bmi323.acc_odr_time_ns :
+				data->bmi323.gyr_odr_time_ns;
+		mutex_unlock(&data->bmi323.mutex);
+	}
+
+	return ret;
+}
+
+static int bmi323_buffer_postenable(struct iio_dev *indio_dev)
+{
+	//struct bmc150_accel_data *data = iio_priv(indio_dev);
+
+	/*
+	 * This code is a placeholder until I can get a way to test it
+	 */
+
+	return 0;
+}
+
+static int bmi323_buffer_predisable(struct iio_dev *indio_dev)
+{
+	//struct bmc150_accel_data *data = iio_priv(indio_dev);
+
+	/*
+	 * This code is a placeholder until I can get a way to test it
+	 */
+
+	return 0;
+}
+
+static int bmi323_buffer_postdisable(struct iio_dev *indio_dev)
+{
+	struct bmc150_accel_data *data = iio_priv(indio_dev);
+
+	return bmi323_set_power_state(&data->bmi323, true);
+}
+
+static const struct iio_buffer_setup_ops bmi323_buffer_ops = {
+	.preenable = bmi323_buffer_preenable,
+	.postenable = bmi323_buffer_postenable,
+	.predisable = bmi323_buffer_predisable,
+	.postdisable = bmi323_buffer_postdisable,
+};
+
+int bmi323_chip_rst(struct bmi323_private_data *bmi323)
+{
+	u16 sensor_status = 0x0000, device_status = 0x0000;
+	int ret;
+
+	ret = bmi323_write_u16(bmi323, BMC150_BMI323_SOFT_RESET_REG,
+			       cpu_to_le16((u16)BMC150_BMI323_SOFT_RESET_VAL));
+	if (ret != 0) {
+		dev_err(bmi323->dev,
+			"bmi323: error while issuing the soft-reset command: %d",
+			ret);
+		return ret;
+	}
+
+	/* wait the specified amount of time... I agree with the bmc150 module: better safe than sorry. */
+	msleep(5);
+
+	// if the device is connected over SPI a dummy read is to be performed once after each reset
+	if (bmi323->spi_client != NULL) {
+		dev_info(bmi323->dev,
+			"issuing the dummy read to switch mode to SPI");
+
+		// do not even check the result of that... it's just a dummy read
+		bmi323_chip_check(bmi323);
+	}
+
+	ret = bmi323_chip_check(bmi323);
+	if (ret != 0) {
+		return ret;
+	}
+
+	/* now check the correct initialization status as per datasheet */
+	ret = bmi323_read_u16(bmi323, 0x01, &device_status);
+	if (ret != 0) {
+		return -EINVAL;
+	}
+
+	if ((device_status & cpu_to_le16((u16)0x00FFU)) !=
+	    cpu_to_le16((u16)0x0000U)) {
+		dev_err(bmi323->dev,
+			"bmi323: device_status incorrect: %d; device_status = 0x%04x",
+			ret, device_status);
+
+		/* from the datasheet: power error */
+		return -EINVAL;
+	}
+
+	/* from the datasheet: power ok */
+	ret = bmi323_read_u16(bmi323, 0x02, &sensor_status);
+	if (ret != 0) {
+		return -EINVAL;
+	}
+
+	if ((sensor_status & cpu_to_le16((u16)0x00FFU)) !=
+	    cpu_to_le16((u16)0x0001U)) {
+		dev_err(bmi323->dev,
+			"bmi323: sensor_status incorrect: %d; sensor_status = 0x%04x",
+			ret, sensor_status);
+
+		/* from the datasheet: initialization error */
+		return -EINVAL;
+	}
+
+	/* from the datasheet: initialization ok */
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(bmi323_chip_rst, IIO_BMC150);
+
+static const struct iio_chan_spec bmi323_channels[] = {
+	BMI323_ACCEL_CHANNEL(X, 16),
+	BMI323_ACCEL_CHANNEL(Y, 16),
+	BMI323_ACCEL_CHANNEL(Z, 16),
+	BMI323_GYRO_CHANNEL(X, 16),
+	BMI323_GYRO_CHANNEL(Y, 16),
+	BMI323_GYRO_CHANNEL(Z, 16),
+	{
+		.type = IIO_TEMP,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_SCALE) |
+				      BIT(IIO_CHAN_INFO_OFFSET),
+		.scan_index = BMI323_TEMP,
+	},
+	IIO_CHAN_SOFT_TIMESTAMP(BMI323_AXIS_MAX),
+};
+
+static int bmi323_read_raw(struct iio_dev *indio_dev,
+			   struct iio_chan_spec const *chan, int *val,
+			   int *val2, long mask)
+{
+	struct bmc150_accel_data *data = iio_priv(indio_dev);
+	int ret = -EINVAL, was_sleep_modified = -1;
+	u16 raw_read = 0x8000;
+
+	mutex_lock(&data->bmi323.mutex);
+
+	if ((data->bmi323.flags & BMI323_FLAGS_RESET_FAILED) != 0x00U) {
+		dev_err(data->bmi323.dev,
+			"bmi323 error: device has not being woken up correctly.");
+		mutex_unlock(&data->bmi323.mutex);
+		return -EBUSY;
+	}
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW: {
+		switch (chan->type) {
+		case IIO_TEMP:
+			if (iio_buffer_enabled(indio_dev)) {
+				ret = -EBUSY;
+				goto bmi323_read_raw_error;
+			}
+
+			was_sleep_modified =
+				bmi323_set_power_state(&data->bmi323, true);
+			if (was_sleep_modified != 0) {
+				ret = was_sleep_modified;
+				goto bmi323_read_raw_error_power;
+			}
+
+			ret = iio_device_claim_direct_mode(indio_dev);
+			if (ret != 0) {
+				printk(KERN_CRIT
+				       "bmc150 bmi323_read_raw IIO_TEMP iio_device_claim_direct_mode returned %d\n",
+				       ret);
+				goto bmi323_read_raw_error;
+			}
+
+			ret = bmi323_read_u16(
+				&data->bmi323,
+				BMC150_BMI323_TEMPERATURE_DATA_REG, &raw_read);
+			iio_device_release_direct_mode(indio_dev);
+			if (ret != 0) {
+				printk(KERN_CRIT
+				       "bmc150 bmi323_read_raw IIO_TEMP bmi323_read_u16 returned %d\n",
+				       ret);
+				goto bmi323_read_raw_error;
+			}
+
+			*val = sign_extend32(le16_to_cpu(raw_read), 15);
+			bmi323_set_power_state(&data->bmi323, false);
+			mutex_unlock(&data->bmi323.mutex);
+			return IIO_VAL_INT;
+
+		case IIO_ACCEL:
+			if (iio_buffer_enabled(indio_dev)) {
+				ret = -EBUSY;
+				goto bmi323_read_raw_error;
+			}
+
+			was_sleep_modified =
+				bmi323_set_power_state(&data->bmi323, true);
+			if (was_sleep_modified != 0) {
+				ret = was_sleep_modified;
+				goto bmi323_read_raw_error_power;
+			}
+
+			ret = iio_device_claim_direct_mode(indio_dev);
+			if (ret != 0) {
+				printk(KERN_CRIT
+				       "bmc150 bmi323_read_raw IIO_ACCEL iio_device_claim_direct_mode returned %d\n",
+				       ret);
+				goto bmi323_read_raw_error;
+			}
+
+			ret = bmi323_read_u16(&data->bmi323,
+					      BMC150_BMI323_DATA_BASE_REG +
+						      (u8)(chan->scan_index),
+					      &raw_read);
+			iio_device_release_direct_mode(indio_dev);
+			if (ret != 0) {
+				printk(KERN_CRIT
+				       "bmc150 bmi323_read_raw IIO_ACCEL bmi323_read_u16 returned %d\n",
+				       ret);
+				goto bmi323_read_raw_error;
+			}
+			*val = sign_extend32(le16_to_cpu(raw_read), 15);
+			bmi323_set_power_state(&data->bmi323, false);
+			mutex_unlock(&data->bmi323.mutex);
+			return IIO_VAL_INT;
+
+		case IIO_ANGL_VEL:
+			if (iio_buffer_enabled(indio_dev)) {
+				ret = -EBUSY;
+				goto bmi323_read_raw_error;
+			}
+
+			was_sleep_modified =
+				bmi323_set_power_state(&data->bmi323, true);
+			if (was_sleep_modified != 0) {
+				ret = was_sleep_modified;
+				goto bmi323_read_raw_error_power;
+			}
+
+			ret = iio_device_claim_direct_mode(indio_dev);
+			if (ret != 0) {
+				printk(KERN_CRIT
+				       "bmc150 bmi323_read_raw IIO_ANGL_VEL iio_device_claim_direct_mode returned %d\n",
+				       ret);
+				goto bmi323_read_raw_error;
+			}
+
+			ret = bmi323_read_u16(&data->bmi323,
+					      BMC150_BMI323_DATA_BASE_REG +
+						      (u8)(chan->scan_index),
+					      &raw_read);
+			iio_device_release_direct_mode(indio_dev);
+			if (ret != 0) {
+				printk(KERN_CRIT
+				       "bmc150 bmi323_read_raw IIO_ANGL_VEL bmi323_read_u16 returned %d\n",
+				       ret);
+				goto bmi323_read_raw_error;
+			}
+
+			*val = sign_extend32(le16_to_cpu(raw_read), 15);
+			bmi323_set_power_state(&data->bmi323, false);
+			mutex_unlock(&data->bmi323.mutex);
+			return IIO_VAL_INT;
+
+		default:
+			goto bmi323_read_raw_error;
+		}
+	}
+	case IIO_CHAN_INFO_OFFSET: {
+		switch (chan->type) {
+		case IIO_TEMP:
+			*val = BMC150_BMI323_TEMPER_CENTER_VAL;
+			*val2 = 0;
+			mutex_unlock(&data->bmi323.mutex);
+			return IIO_VAL_INT;
+
+		default:
+			ret = -EINVAL;
+			goto bmi323_read_raw_error;
+		}
+	}
+	case IIO_CHAN_INFO_SCALE:
+		switch (chan->type) {
+		case IIO_TEMP: {
+			*val = 0;
+			*val2 = BMC150_BMI323_TEMPER_LSB_PER_KELVIN_VAL;
+			mutex_unlock(&data->bmi323.mutex);
+			return IIO_VAL_FRACTIONAL;
+		}
+		case IIO_ACCEL: {
+			u8 *le_raw_read =
+				(u8 *)&data->bmi323.acc_conf_reg_value;
+			for (int s = 0; s < ARRAY_SIZE(bmi323_accel_scale_map);
+			     ++s) {
+				if (((le_raw_read[0]) & ((u16)0b01110000U)) ==
+				    (bmi323_accel_scale_map[s].hw_val)) {
+					*val = bmi323_accel_scale_map[s].val;
+					*val2 = bmi323_accel_scale_map[s].val2;
+
+					mutex_unlock(&data->bmi323.mutex);
+					return bmi323_accel_scale_map[s]
+						.ret_type;
+				}
+			}
+
+			ret = -EINVAL;
+			goto bmi323_read_raw_error;
+		}
+		case IIO_ANGL_VEL: {
+			u8 *le_raw_read =
+				(u8 *)&data->bmi323.gyr_conf_reg_value;
+			for (int s = 0; s < ARRAY_SIZE(bmi323_gyro_scale_map);
+			     ++s) {
+				if (((le_raw_read[0]) & ((u16)0b01110000U)) ==
+				    (bmi323_gyro_scale_map[s].hw_val)) {
+					*val = bmi323_gyro_scale_map[s].val;
+					*val2 = bmi323_gyro_scale_map[s].val2;
+
+					mutex_unlock(&data->bmi323.mutex);
+					return bmi323_gyro_scale_map[s].ret_type;
+				}
+			}
+
+			ret = -EINVAL;
+			goto bmi323_read_raw_error;
+		}
+		default:
+			ret = -EINVAL;
+			goto bmi323_read_raw_error;
+		}
+	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
+		switch (chan->type) {
+		case IIO_ACCEL: {
+			u8 *le_raw_read =
+				(u8 *)&data->bmi323.acc_conf_reg_value;
+			for (int s = 0; s < ARRAY_SIZE(bmi323_accel_odr_map);
+			     ++s) {
+				if (((le_raw_read[0]) & ((u16)0x0FU)) ==
+				    (bmi323_accel_odr_map[s].hw_val)) {
+					/*
+							 * from tha datasheed: -3dB cut-off frequency can be configured with the bit 7 of GYR_confm,
+							 * also called acc_bw that can either be 0 or 1, where 1 means odr/4 and 0 means odr/2
+							 */
+					int freq_adj_idx =
+						(((le_raw_read[0]) &
+						  ((u8)0x80U)) == (u8)0x00U) ?
+							(s * 2) + 0 :
+							(s * 2) + 1;
+					*val = bmi323_accel_3db_freq_cutoff
+						       [freq_adj_idx]
+							       .val;
+					*val2 = bmi323_accel_3db_freq_cutoff
+							[freq_adj_idx]
+								.val2;
+
+					mutex_unlock(&data->bmi323.mutex);
+					return IIO_VAL_INT_PLUS_MICRO;
+				}
+			}
+
+			ret = -EINVAL;
+			goto bmi323_read_raw_error;
+		}
+		case IIO_ANGL_VEL: {
+			u8 *le_raw_read =
+				(u8 *)&data->bmi323.gyr_conf_reg_value;
+			for (int s = 0; s < ARRAY_SIZE(bmi323_gyro_odr_map);
+			     ++s) {
+				if (((le_raw_read[0]) & ((u16)0x0FU)) ==
+				    (bmi323_gyro_odr_map[s].hw_val)) {
+					/*
+							 * from tha datasheed: -3dB cut-off frequency can be configured with the bit 7 of GYR_confm,
+							 * also called acc_bw that can either be 0 or 1, where 1 means odr/4 and 0 means odr/2
+							 */
+					int freq_adj_idx =
+						(((le_raw_read[0]) &
+						  ((u8)0x80U)) == (u8)0x0000U) ?
+							(s * 2) + 0 :
+							(s * 2) + 1;
+					*val = bmi323_gyro_3db_freq_cutoff
+						       [freq_adj_idx]
+							       .val;
+					*val2 = bmi323_gyro_3db_freq_cutoff
+							[freq_adj_idx]
+								.val2;
+
+					mutex_unlock(&data->bmi323.mutex);
+					return bmi323_gyro_3db_freq_cutoff
+						[freq_adj_idx]
+							.ret_type;
+				}
+			}
+
+			ret = -EINVAL;
+			goto bmi323_read_raw_error;
+		}
+		default: {
+			ret = -EINVAL;
+			goto bmi323_read_raw_error;
+		}
+		}
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		switch (chan->type) {
+		case IIO_TEMP: {
+
+			// while in normal or power mode the temperature sensur has a 50Hz sampling frequency
+			*val = 50;
+			*val2 = 0;
+
+			mutex_unlock(&data->bmi323.mutex);
+			return IIO_VAL_INT_PLUS_MICRO;
+		}
+		case IIO_ACCEL: {
+			u8 *le_raw_read =
+				(u8 *)&data->bmi323.acc_conf_reg_value;
+			for (int s = 0; s < ARRAY_SIZE(bmi323_accel_odr_map);
+			     ++s) {
+				if (((le_raw_read[0]) & ((u16)0x0FU)) ==
+				    (bmi323_accel_odr_map[s].hw_val)) {
+					*val = bmi323_accel_odr_map[s].val;
+					*val2 = bmi323_accel_odr_map[s].val2;
+
+					mutex_unlock(&data->bmi323.mutex);
+					return IIO_VAL_INT_PLUS_MICRO;
+				}
+			}
+
+			ret = -EINVAL;
+			goto bmi323_read_raw_error;
+		}
+		case IIO_ANGL_VEL: {
+			u8 *le_raw_read =
+				(u8 *)&data->bmi323.gyr_conf_reg_value;
+			for (int s = 0; s < ARRAY_SIZE(bmi323_gyro_odr_map);
+			     ++s) {
+				if (((le_raw_read[0]) & ((u16)0x0FU)) ==
+				    (bmi323_gyro_odr_map[s].hw_val)) {
+					*val = bmi323_gyro_odr_map[s].val;
+					*val2 = bmi323_gyro_odr_map[s].val2;
+
+					mutex_unlock(&data->bmi323.mutex);
+					return IIO_VAL_INT_PLUS_MICRO;
+				}
+			}
+
+			ret = -EINVAL;
+			goto bmi323_read_raw_error;
+		}
+		default:
+			ret = -EINVAL;
+			goto bmi323_read_raw_error;
+		}
+	default:
+		ret = -EINVAL;
+		goto bmi323_read_raw_error;
+	}
+
+bmi323_read_raw_error:
+	if (was_sleep_modified == 0) {
+		bmi323_set_power_state(&data->bmi323, false);
+	}
+
+bmi323_read_raw_error_power:
+	mutex_unlock(&data->bmi323.mutex);
+	return ret;
+}
+
+static int bmi323_write_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan, int val, int val2,
+			    long mask)
+{
+	struct bmc150_accel_data *data = iio_priv(indio_dev);
+	int ret = -EINVAL, was_sleep_modified = -1;
+
+	mutex_lock(&data->bmi323.mutex);
+
+	if ((data->bmi323.flags & BMI323_FLAGS_RESET_FAILED) != 0x00U) {
+		dev_err(data->bmi323.dev,
+			"bmi323 error: device has not being woken up correctly.");
+		mutex_unlock(&data->bmi323.mutex);
+		return -EBUSY;
+	}
+
+	switch (mask) {
+	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
+		switch (chan->type) {
+		default: {
+			ret = -EINVAL;
+			goto bmi323_write_raw_error;
+		}
+		}
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		switch (chan->type) {
+		case IIO_ACCEL:
+			if (iio_buffer_enabled(indio_dev)) {
+				ret = -EBUSY;
+				goto bmi323_write_raw_error;
+			}
+
+			for (int s = 0; s < ARRAY_SIZE(bmi323_accel_odr_map);
+			     ++s) {
+				if ((bmi323_accel_odr_map[s].val == val) &&
+				    (bmi323_accel_odr_map[s].val2 == val2)) {
+					const u16 conf_backup =
+						data->bmi323.acc_conf_reg_value;
+					u8 *le_raw_read =
+						(u8 *)&data->bmi323
+							.acc_conf_reg_value;
+					le_raw_read[0] &= (u8)0b11110000U;
+					le_raw_read[0] |=
+						((u8)bmi323_gyro_odr_map[s]
+							 .hw_val);
+
+					was_sleep_modified =
+						bmi323_set_power_state(
+							&data->bmi323, true);
+					if (was_sleep_modified != 0) {
+						ret = was_sleep_modified;
+						data->bmi323.acc_conf_reg_value =
+							conf_backup;
+						goto bmi323_write_raw_error_power;
+					}
+
+					ret = bmi323_write_u16(
+						&data->bmi323,
+						BMC150_BMI323_ACC_CONF_REG,
+						data->bmi323.acc_conf_reg_value);
+					if (ret != 0) {
+						data->bmi323.acc_conf_reg_value =
+							conf_backup;
+						goto bmi323_write_raw_error;
+					}
+
+					data->bmi323.acc_odr_time_ns =
+						bmi323_accel_odr_map[s].time_ns;
+					bmi323_set_power_state(&data->bmi323,
+							       false);
+					mutex_unlock(&data->bmi323.mutex);
+					return 0;
+				}
+			}
+
+			ret = -EINVAL;
+			goto bmi323_write_raw_error;
+		case IIO_ANGL_VEL:
+			if (iio_buffer_enabled(indio_dev)) {
+				ret = -EBUSY;
+				goto bmi323_write_raw_error;
+			}
+
+			for (int s = 0; s < ARRAY_SIZE(bmi323_gyro_odr_map);
+			     ++s) {
+				if ((bmi323_gyro_odr_map[s].val == val) &&
+				    (bmi323_gyro_odr_map[s].val2 == val2)) {
+					const u16 conf_backup =
+						data->bmi323.gyr_conf_reg_value;
+					u8 *le_raw_read =
+						(u8 *)&data->bmi323
+							.gyr_conf_reg_value;
+					le_raw_read[0] &= (u8)0b11110000U;
+					le_raw_read[0] |=
+						((u8)bmi323_gyro_odr_map[s]
+							 .hw_val);
+
+					was_sleep_modified =
+						bmi323_set_power_state(
+							&data->bmi323, true);
+					if (was_sleep_modified != 0) {
+						ret = was_sleep_modified;
+						data->bmi323.gyr_conf_reg_value =
+							conf_backup;
+						goto bmi323_write_raw_error_power;
+					}
+
+					ret = bmi323_write_u16(
+						&data->bmi323,
+						BMC150_BMI323_GYR_CONF_REG,
+						data->bmi323.gyr_conf_reg_value);
+					if (ret != 0) {
+						data->bmi323.gyr_conf_reg_value =
+							conf_backup;
+						goto bmi323_write_raw_error;
+					}
+
+					data->bmi323.gyr_odr_time_ns =
+						bmi323_gyro_odr_map[s].time_ns;
+					bmi323_set_power_state(&data->bmi323,
+							       false);
+					mutex_unlock(&data->bmi323.mutex);
+					return 0;
+				}
+			}
+
+			ret = -EINVAL;
+			goto bmi323_write_raw_error;
+
+		/* Termometer also ends up here: its sampling frequency depends on the chip configuration and cannot be changed */
+		default:
+			ret = -EINVAL;
+			goto bmi323_write_raw_error;
+		}
+
+		break;
+	case IIO_CHAN_INFO_SCALE:
+		switch (chan->type) {
+		case IIO_ACCEL:
+			if (iio_buffer_enabled(indio_dev)) {
+				ret = -EBUSY;
+				goto bmi323_write_raw_error;
+			}
+
+			for (int s = 0; s < ARRAY_SIZE(bmi323_accel_scale_map);
+			     ++s) {
+				if ((bmi323_accel_scale_map[s].val == val) &&
+				    (bmi323_accel_scale_map[s].val2 == val2)) {
+					u8 *le_raw_read =
+						(u8 *)&data->bmi323
+							.acc_conf_reg_value;
+					le_raw_read[0] &= (u8)0b10001111U;
+					le_raw_read[0] |=
+						((u8)bmi323_accel_scale_map[s]
+							 .hw_val);
+
+					was_sleep_modified =
+						bmi323_set_power_state(
+							&data->bmi323, true);
+					if (was_sleep_modified != 0) {
+						ret = was_sleep_modified;
+						goto bmi323_write_raw_error_power;
+					}
+
+					ret = bmi323_write_u16(
+						&data->bmi323,
+						BMC150_BMI323_ACC_CONF_REG,
+						data->bmi323.acc_conf_reg_value);
+					if (ret != 0) {
+						goto bmi323_write_raw_error;
+					}
+
+					bmi323_set_power_state(&data->bmi323,
+							       false);
+					mutex_unlock(&data->bmi323.mutex);
+					return 0;
+				}
+			}
+
+			dev_warn(
+				data->bmi323.dev,
+				"bmi323 error: accel scale val=%d,val2=%d unavailable: ignoring.",
+				val, val2);
+
+			ret = -EINVAL;
+			goto bmi323_write_raw_error;
+		case IIO_ANGL_VEL:
+			if (iio_buffer_enabled(indio_dev)) {
+				ret = -EBUSY;
+				goto bmi323_write_raw_error;
+			}
+
+			for (int s = 0; s < ARRAY_SIZE(bmi323_gyro_scale_map);
+			     ++s) {
+				if ((bmi323_gyro_scale_map[s].val == val) &&
+				    (bmi323_gyro_scale_map[s].val2 == val2)) {
+					u8 *le_raw_read =
+						(u8 *)&data->bmi323
+							.gyr_conf_reg_value;
+					le_raw_read[0] &= (u8)0b10001111U;
+					le_raw_read[0] |=
+						((u8)bmi323_gyro_scale_map[s]
+							 .hw_val);
+
+					was_sleep_modified =
+						bmi323_set_power_state(
+							&data->bmi323, true);
+					if (was_sleep_modified != 0) {
+						ret = was_sleep_modified;
+						goto bmi323_write_raw_error_power;
+					}
+
+					ret = bmi323_write_u16(
+						&data->bmi323,
+						BMC150_BMI323_GYR_CONF_REG,
+						data->bmi323.acc_conf_reg_value);
+					if (ret != 0) {
+						goto bmi323_write_raw_error;
+					}
+
+					bmi323_set_power_state(&data->bmi323,
+							       false);
+					mutex_unlock(&data->bmi323.mutex);
+					return 0;
+				}
+			}
+
+			dev_warn(
+				data->bmi323.dev,
+				"bmi323 error: gyro scale val=%d,val2=%d unavailable: ignoring.",
+				val, val2);
+
+			ret = -EINVAL;
+			goto bmi323_write_raw_error;
+
+		default:
+			ret = -EINVAL;
+			goto bmi323_write_raw_error;
+		}
+
+	default:
+		ret = -EINVAL;
+		goto bmi323_write_raw_error;
+	}
+
+bmi323_write_raw_error:
+	if (was_sleep_modified == 0) {
+		bmi323_set_power_state(&data->bmi323, false);
+	}
+
+bmi323_write_raw_error_power:
+	mutex_unlock(&data->bmi323.mutex);
+	return ret;
+}
+
+static int bmi323_read_avail(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan, const int **vals,
+			     int *type, int *length, long mask)
+{
+	switch (mask) {
+	case IIO_CHAN_INFO_SCALE:
+		switch (chan->type) {
+		case IIO_ACCEL:
+			*type = IIO_VAL_INT_PLUS_MICRO;
+			*vals = bmi323_accel_scales;
+			*length = ARRAY_SIZE(bmi323_accel_scales);
+			return IIO_AVAIL_LIST;
+		case IIO_ANGL_VEL:
+			*type = IIO_VAL_INT_PLUS_NANO;
+			*vals = bmi323_gyro_scales;
+			*length = ARRAY_SIZE(bmi323_gyro_scales);
+			return IIO_AVAIL_LIST;
+		default:
+			return -EINVAL;
+		}
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		*type = IIO_VAL_INT_PLUS_MICRO;
+		*vals = bmi323_sample_freqs;
+		*length = ARRAY_SIZE(bmi323_sample_freqs);
+		return IIO_AVAIL_LIST;
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct iio_info bmi323_accel_info = {
+	.read_raw = bmi323_read_raw,
+	.write_raw = bmi323_write_raw,
+	.read_avail = bmi323_read_avail,
+	//.hwfifo_flush_to_buffer	= bmi323_fifo_flush,
+};
+
+static int bmi323_fifo_flush(struct iio_dev *indio_dev)
+{
+	struct bmc150_accel_data *data = iio_priv(indio_dev);
+	int ret;
+
+	ret = bmi323_write_u16(&data->bmi323, 0x37, cpu_to_le16(0x01));
+
+	return ret;
+}
+
+static const u16 stub_value = 0x8000;
+
+#define ADVANCE_AT_REQ_OR_AVAIL(req, avail, dst, dst_offset, src, src_offset) \
+	if (req) {                                                            \
+		if (gyr_avail) {                                              \
+			memcpy((void *)(dst + dst_offset),                    \
+			       (const void *)(src + src_offset), 2);          \
+			src_offset += 2;                                      \
+		} else {                                                      \
+			memcpy((void *)(dst + dst_offset),                    \
+			       (const void *)((const u8 *)(&stub_value)), 2); \
+		}                                                             \
+		dst_offset += 2;                                              \
+	} else {                                                              \
+		if (avail) {                                                  \
+			src_offset += 2;                                      \
+		}                                                             \
+	}
+
+static irqreturn_t iio_bmi323_trigger_h(int irq, void *p)
+{
+	printk(KERN_WARNING "bmi323 executed iio_bmi323_trigger_h");
+
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct bmc150_accel_data *indio_data = iio_priv(indio_dev);
+
+	mutex_lock(&indio_data->bmi323.mutex);
+
+	const bool temp_avail = ((indio_data->bmi323.fifo_conf_reg_value &
+				  (cpu_to_le16(0b0000100000000000))) != 0);
+	const bool gyr_avail = ((indio_data->bmi323.fifo_conf_reg_value &
+				 (cpu_to_le16(0b0000010000000000))) != 0);
+	const bool acc_avail = ((indio_data->bmi323.fifo_conf_reg_value &
+				 (cpu_to_le16(0b0000001000000000))) != 0);
+	const bool time_avail = ((indio_data->bmi323.fifo_conf_reg_value &
+				  (cpu_to_le16(0b0000000100000000))) != 0);
+
+	/* Calculate the number of bytes for a frame */
+	const u16 frames_aggregate_size_in_words =
+		/* 2 * */ ((temp_avail ? 1 : 0) + (gyr_avail ? 3 : 0) +
+			   (acc_avail ? 3 : 0) + (time_avail ? 1 : 0));
+
+	u16 available_words = 0;
+	const int available_words_read_res = bmi323_read_u16(
+		&indio_data->bmi323, BMC150_BMI323_FIFO_FILL_LEVEL_REG,
+		&available_words);
+	if (available_words_read_res != 0) {
+		goto bmi323_irq_done;
+	}
+
+	const u16 available_frame_aggregates = (le16_to_cpu(available_words)) /
+					       (frames_aggregate_size_in_words);
+
+	const s64 current_timestamp_ns = iio_get_time_ns(indio_dev);
+	const s64 fifo_frame_time_ns =
+		indio_data->bmi323.fifo_frame_time_diff_ns;
+	const s64 first_sample_timestamp_ns =
+		current_timestamp_ns -
+		(fifo_frame_time_ns * (s64)(available_frame_aggregates));
+
+	/* This can hold one full block */
+	u8 temp_data[16];
+
+	/* This is fifo data as read from the sensor */
+	u8 fifo_data[32];
+
+	/*
+	| CHANNEL		|	scan_index
+	|============================
+	|				|			|
+	| ACCEL_X		|		0	|
+	| ACCEL_Y		|		1	|
+	| ACCEL_Y		|		2	|
+	| GYRO_X		|		3	|
+	| GYRO_Y		|		4	|
+	| GYRO_Z		|		5	|
+	| TEMP			|		6	|
+	| TIMESTAMP		|		?	|
+	*/
+	bool accel_x_requested = false;
+	bool accel_y_requested = false;
+	bool accel_z_requested = false;
+	bool gyro_x_requested = false;
+	bool gyro_y_requested = false;
+	bool gyro_z_requested = false;
+	bool temp_requested = false;
+
+	int j = 0;
+	for_each_set_bit(j, indio_dev->active_scan_mask,
+			 indio_dev->masklength) {
+		switch (j) {
+		case 0:
+			accel_x_requested = true;
+			break;
+		case 1:
+			accel_y_requested = true;
+			break;
+		case 2:
+			accel_z_requested = true;
+			break;
+		case 3:
+			gyro_x_requested = true;
+			break;
+		case 4:
+			gyro_y_requested = true;
+			break;
+		case 5:
+			gyro_z_requested = true;
+			break;
+		case 6:
+			temp_requested = true;
+			break;
+		default:
+			break;
+		}
+	}
+
+	u16 current_fifo_buffer_offset_bytes = 0;
+	for (u16 f = 0; f < available_frame_aggregates; ++f) {
+		u16 current_sample_buffer_offset = 0;
+
+		/* Read data from the raw device */
+		if (indio_data->bmi323.i2c_client != NULL) {
+			const int bytes_to_read =
+				2 + (2 * frames_aggregate_size_in_words);
+			int read_block_ret = i2c_smbus_read_i2c_block_data(
+				indio_data->bmi323.i2c_client,
+				BMC150_BMI323_FIFO_DATA_REG, bytes_to_read,
+				&fifo_data[0]);
+			if (read_block_ret < bytes_to_read) {
+				dev_warn(
+					&indio_data->bmi323.i2c_client->dev,
+					"bmi323: i2c_smbus_read_i2c_block_data wrong return: expected %d bytes, %d arrived. Doing what is possible with recovered data.\n",
+					bytes_to_read, read_block_ret);
+
+				/* at this point FIFO buffer must be flushed to avoid interpreting data incorrectly the next trigger */
+				const int flush_res =
+					bmi323_fifo_flush(indio_dev);
+				if (flush_res != 0) {
+					dev_err(&indio_data->bmi323.i2c_client
+							 ->dev,
+						"bmi323: Could not flush FIFO (%d). Following buffer data might be corrupted.\n",
+						flush_res);
+				}
+
+				goto bmi323_irq_done;
+			}
+
+			/* Discard 2-bytes dummy data from I2C */
+			current_fifo_buffer_offset_bytes = 2;
+		} else if (indio_data->bmi323.spi_client != NULL) {
+			printk(KERN_CRIT
+			       "bmi323: SPI interface is not yet implemented.\n");
+
+			/*
+			* To whoever may need this: implementing this should be straightforward:
+			* it's specular to the i2c part.
+			*/
+
+			/* Discard 1-byte dummy data from SPI */
+			current_fifo_buffer_offset_bytes = 1;
+
+			goto bmi323_irq_done;
+		}
+
+		ADVANCE_AT_REQ_OR_AVAIL(accel_x_requested, acc_avail,
+					(u8 *)&temp_data[0],
+					current_sample_buffer_offset,
+					(u8 *)&fifo_data[0],
+					current_fifo_buffer_offset_bytes);
+		ADVANCE_AT_REQ_OR_AVAIL(accel_y_requested, acc_avail,
+					(u8 *)&temp_data[0],
+					current_sample_buffer_offset,
+					(u8 *)&fifo_data[0],
+					current_fifo_buffer_offset_bytes);
+		ADVANCE_AT_REQ_OR_AVAIL(accel_z_requested, acc_avail,
+					(u8 *)&temp_data[0],
+					current_sample_buffer_offset,
+					(u8 *)&fifo_data[0],
+					current_fifo_buffer_offset_bytes);
+		ADVANCE_AT_REQ_OR_AVAIL(gyro_x_requested, gyr_avail,
+					(u8 *)&temp_data[0],
+					current_sample_buffer_offset,
+					(u8 *)&fifo_data[0],
+					current_fifo_buffer_offset_bytes);
+		ADVANCE_AT_REQ_OR_AVAIL(gyro_y_requested, gyr_avail,
+					(u8 *)&temp_data[0],
+					current_sample_buffer_offset,
+					(u8 *)&fifo_data[0],
+					current_fifo_buffer_offset_bytes);
+		ADVANCE_AT_REQ_OR_AVAIL(gyro_z_requested, gyr_avail,
+					(u8 *)&temp_data[0],
+					current_sample_buffer_offset,
+					(u8 *)&fifo_data[0],
+					current_fifo_buffer_offset_bytes);
+		ADVANCE_AT_REQ_OR_AVAIL(temp_requested, temp_avail,
+					(u8 *)&temp_data[0],
+					current_sample_buffer_offset,
+					(u8 *)&fifo_data[0],
+					current_fifo_buffer_offset_bytes);
+
+#ifdef BMC150_BMI232_DEBUG_EN
+		/* The following is code only used for debugging */
+		u16 timestamp = 0;
+		if (time_avail) {
+			memcpy((u8 *)&timestamp,
+			       (const u8
+					*)(&fifo_data
+						   [current_fifo_buffer_offset_bytes]),
+			       2);
+			current_fifo_buffer_offset_bytes += 2;
+		}
+
+		u16 *debg = (u16 *)&temp_data[0];
+		if (!time_avail) {
+			printk(KERN_WARNING
+			       "bmi323 pushing to buffer %d/%d -- accel: %d %d %d gyro: %d %d %d",
+			       (int)(f + 1), (int)available_frame_aggregates,
+			       (int)(*((s16 *)&debg[0])),
+			       (int)(*((s16 *)&debg[1])),
+			       (int)(*((s16 *)&debg[2])),
+			       (int)(*((s16 *)&debg[3])),
+			       (int)(*((s16 *)&debg[4])),
+			       (int)(*((s16 *)&debg[5])));
+		} else {
+			printk(KERN_WARNING
+			       "bmi323 pushing to buffer %d/%d -- time: %d accel: %d %d %d gyro: %d %d %d",
+			       (int)(f + 1), (int)available_frame_aggregates,
+			       (int)timestamp, (int)(*((s16 *)&debg[0])),
+			       (int)(*((s16 *)&debg[1])),
+			       (int)(*((s16 *)&debg[2])),
+			       (int)(*((s16 *)&debg[3])),
+			       (int)(*((s16 *)&debg[4])),
+			       (int)(*((s16 *)&debg[5])));
+		}
+#endif
+
+		iio_push_to_buffers_with_timestamp(
+			indio_dev, &temp_data[0],
+			first_sample_timestamp_ns +
+				(fifo_frame_time_ns * (s64)j));
+	}
+
+bmi323_irq_done:
+	mutex_unlock(&indio_data->bmi323.mutex);
+
+	/*
+	 * Tell the core we are done with this trigger and ready for the
+	 * next one.
+	 */
+	iio_trigger_notify_done(indio_dev->trig);
+
+	return IRQ_HANDLED;
+}
+
+int bmi323_set_trigger_state(struct iio_trigger *trig, bool state)
+{
+	return 0;
+}
+
+/*
+// The following is meant to be used in a IRQ-enabled hardware
+static  const struct iio_trigger_ops time_trigger_ops = {
+    .set_trigger_state = &bmi323_set_trigger_state,
+    //.reenable = NULL,
+    .validate_device = &iio_trigger_validate_own_device,
+};
+*/
+
+/*
+ * A very basic scan mask: everything can work in conjunction with everything else so no need to worry about
+ * managing conbinations of mutually exclusive data sources...
+ */
+static const unsigned long bmi323_accel_scan_masks[] = {
+	BIT(BMI323_ACCEL_AXIS_X) | BIT(BMI323_ACCEL_AXIS_Y) |
+		BIT(BMI323_ACCEL_AXIS_Z) | BIT(BMI323_GYRO_AXIS_X) |
+		BIT(BMI323_GYRO_AXIS_Y) |
+		BIT(BMI323_GYRO_AXIS_Z) /*| BIT(BMI323_TEMP)*/,
+	0
+};
+
+int bmi323_iio_init(struct iio_dev *indio_dev)
+{
+	struct bmc150_accel_data *data = iio_priv(indio_dev);
+	struct irq_data *irq_desc = NULL;
+
+	if (data->bmi323.i2c_client != NULL) {
+		data->bmi323.dev = &data->bmi323.i2c_client->dev;
+	} else if (data->bmi323.spi_client != NULL) {
+		data->bmi323.dev = &data->bmi323.spi_client->dev;
+	} else {
+		return -ENODEV;
+	}
+
+	int ret = 0;
+
+	/* change to 8 for a default 200Hz sampling rate */
+	const int gyr_odr_conf_idx = 7;
+	const int acc_odr_conf_idx = 7;
+
+	mutex_init(&data->bmi323.mutex);
+
+	data->bmi323.acc_odr_time_ns =
+		bmi323_accel_odr_map[acc_odr_conf_idx].time_ns;
+	data->bmi323.gyr_odr_time_ns =
+		bmi323_gyro_odr_map[gyr_odr_conf_idx].time_ns;
+
+	// FIFO enabled for gyro, accel and temp. Overwrite older samples.
+	data->bmi323.fifo_conf_reg_value = cpu_to_le16((u16)0x0F00U);
+	//data->bmi323.fifo_conf_reg_value = cpu_to_le16((u16)0x0E00U);
+	//data->bmi323.fifo_conf_reg_value = cpu_to_le16((u16)0x0600U); // working
+
+	// now set the (default) normal mode...
+	// normal mode: 0x4000
+	// no averaging: 0x0000
+	data->bmi323.acc_conf_reg_value = cpu_to_le16(
+		0x4000 | ((u16)BMC150_BMI323_ACCEL_RANGE_2_VAL << (u16)4U) |
+		((u16)bmi323_accel_odr_map[acc_odr_conf_idx].hw_val));
+
+	// now set the (default) normal mode...
+	// normal mode: 0x4000
+	// no averaging: 0x0000
+	// filtering to ODR/2: 0x0000
+	data->bmi323.gyr_conf_reg_value = cpu_to_le16(
+		0x4000 | ((u16)BMC150_BMI323_GYRO_RANGE_125_VAL << (u16)4U) |
+		((u16)bmi323_gyro_odr_map[gyr_odr_conf_idx].hw_val));
+
+	// the datasheet states that FIFO buffer MUST be enabled before enabling any sensor
+	ret = bmi323_write_u16(&data->bmi323, BMC150_BMI323_FIFO_CONF_REG,
+			       data->bmi323.fifo_conf_reg_value);
+	if (ret != 0) {
+		return -1;
+	}
+
+	ret = bmi323_write_u16(&data->bmi323, BMC150_BMI323_ACC_CONF_REG,
+			       data->bmi323.acc_conf_reg_value);
+	if (ret != 0) {
+		return -1;
+	}
+
+	ret = bmi323_write_u16(&data->bmi323, BMC150_BMI323_GYR_CONF_REG,
+			       data->bmi323.gyr_conf_reg_value);
+	if (ret != 0) {
+		return -2;
+	}
+
+	indio_dev->channels = bmi323_channels;
+	indio_dev->num_channels = ARRAY_SIZE(bmi323_channels);
+	indio_dev->name = "bmi323";
+	indio_dev->available_scan_masks = bmi323_accel_scan_masks;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->info = &bmi323_accel_info;
+	indio_dev->label = "bmi323-accel_base";
+
+	if (data->bmi323.irq > 0) {
+		dev_info(data->bmi323.dev, "IRQ pin reported as connected: %d",
+			 data->bmi323.irq);
+
+		irq_desc = irq_get_irq_data(data->bmi323.irq);
+		if (!irq_desc) {
+			dev_err(data->bmi323.dev,
+				"Could not find IRQ %d. ignoring it.\n",
+				data->bmi323.irq);
+			goto bmi323_iio_init_missing_irq_pin;
+		}
+
+		//data->bmi323.trig[0] = devm_iio_trigger_alloc(data->bmi323.dev, "trig-fifo_full-%s-%d", indio_dev->name, iio_device_id(indio_dev));
+		//if (data->bmi323.trig[0] == NULL) {
+		//	ret = -ENOMEM;
+		//	goto bmi323_iio_init_err_trigger_unregister;
+		//}
+		//
+		//data->bmi323.trig[0]->ops = &time_trigger_ops;
+		//iio_trigger_set_drvdata(data->bmi323.trig[0], indio_dev);
+		//ret = devm_iio_trigger_register(data->bmi323.dev, data->bmi323.trig[0]);
+		//if (ret) {
+		//	dev_err(data->bmi323.dev, "iio trigger register failed\n");
+		//	goto bmi323_iio_init_err_trigger_unregister;
+		//}
+
+		/*
+		 * register triggers BEFORE buffer setup so that they are cleared
+		 * on emergence exit by bmi323_iio_init_err_trigger_unregister.
+		 *
+		 * This is just a placeholder until I can get my hands on a bmi323
+		 * device that has the IRQ pin actually connected to the CPU.
+		 */
+
+		/* here resume operation with the module part common to irq and non-irq enabled code. */
+		goto bmi323_iio_init_common_irq_and_not_irq;
+	}
+
+bmi323_iio_init_missing_irq_pin:
+	dev_info(
+		data->bmi323.dev,
+		"IRQ pin NOT connected (irq=%d). Will continue normally without triggers.",
+		data->bmi323.irq);
+
+bmi323_iio_init_common_irq_and_not_irq:
+
+	/* Once orientation matrix is implemented switch this to iio_triggered_buffer_setup_ext. */
+	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
+					 iio_bmi323_trigger_h,
+					 &bmi323_buffer_ops);
+	if (ret < 0) {
+		dev_err(data->bmi323.dev,
+			"Failed: iio triggered buffer setup: %d\n", ret);
+		goto bmi323_iio_init_err_trigger_unregister;
+	}
+
+	ret = pm_runtime_set_active(data->bmi323.dev);
+	if (ret) {
+		dev_err(data->bmi323.dev,
+			"bmi323 unable to initialize runtime PD: pm_runtime_set_active returned %d\n",
+			ret);
+		goto bmi323_iio_init_err_buffer_cleanup;
+	}
+
+	pm_runtime_enable(data->bmi323.dev);
+	pm_runtime_set_autosuspend_delay(data->bmi323.dev,
+					 BMC150_BMI323_AUTO_SUSPEND_DELAY_MS);
+	pm_runtime_use_autosuspend(data->bmi323.dev);
+
+	ret = iio_device_register(indio_dev);
+	if (ret < 0) {
+		dev_err(data->bmi323.dev,
+			"bmi323 unable to register iio device: %d\n", ret);
+		goto bmi323_iio_init_err_pm_cleanup;
+	}
+
+	return 0;
+
+bmi323_iio_init_err_pm_cleanup:
+	pm_runtime_dont_use_autosuspend(data->bmi323.dev);
+	pm_runtime_disable(data->bmi323.dev);
+bmi323_iio_init_err_buffer_cleanup:
+	iio_triggered_buffer_cleanup(indio_dev);
+bmi323_iio_init_err_trigger_unregister:
+	/*
+	 * unregister triggers if they have been setup already.
+	 * iio_trigger_unregister shall be used in that regard.
+	 *
+	 * This is just a placeholder until I can get my hands on a bmi323
+	 * device that has the IRQ pin actually connected to the CPU.
+	 */
+	//if (data->bmi323.trig[0] != NULL) {
+	//	iio_trigger_unregister(data->bmi323.trig[0]);
+	//}
+
+	return ret;
+}
+EXPORT_SYMBOL_NS_GPL(bmi323_iio_init, IIO_BMC150);
+
+void bmi323_iio_deinit(struct iio_dev *indio_dev)
+{
+	struct bmc150_accel_data *data = iio_priv(indio_dev);
+	struct device *dev = bmi323_get_managed_device(&data->bmi323);
+
+	iio_device_unregister(indio_dev);
+
+	pm_runtime_disable(dev);
+	pm_runtime_set_suspended(dev);
+	pm_runtime_put_noidle(dev);
+
+	iio_triggered_buffer_cleanup(indio_dev);
+
+	//iio_device_free(indio_dev); // this isn't done in the bmg160 driver nor in other drivers so I guess I shouldn't do it too
+
+	mutex_unlock(&data->bmi323.mutex);
+	bmi323_chip_rst(&data->bmi323);
+	mutex_unlock(&data->bmi323.mutex);
+}
+EXPORT_SYMBOL_NS_GPL(bmi323_iio_deinit, IIO_BMC150);
+
+#ifdef CONFIG_PM_SLEEP
+static int bmc150_accel_suspend(struct device *dev)
 {
 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
 	struct bmc150_accel_data *data = iio_priv(indio_dev);
 
+	if (data->dev_type == BMI323) {
+		int ret;
+
+		//dev_warn(dev, "bmi323 suspending driver...");
+
+		// here push the register GYRO & ACCEL configuration and issue a reset so that chip goes to sleep mode (the default one after a reset)
+		mutex_unlock(&data->bmi323.mutex);
+
+		ret = bmi323_chip_rst(&data->bmi323);
+		mutex_unlock(&data->bmi323.mutex);
+		if (ret != 0) {
+			dev_err(dev,
+				"bmi323 error in suspend on bmi323_chip_rst: %d\n",
+				ret);
+			data->bmi323.flags |= BMI323_FLAGS_RESET_FAILED;
+			return -EAGAIN;
+		}
+
+		return 0;
+	}
+
 	mutex_lock(&data->mutex);
 	bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
 	mutex_unlock(&data->mutex);
@@ -1844,6 +4005,63 @@ static int bmc150_accel_resume(struct device *dev)
 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
 	struct bmc150_accel_data *data = iio_priv(indio_dev);
 
+	if (data->dev_type == BMI323) {
+		int ret;
+
+		//dev_warn(dev, "bmi323 resuming driver...");
+
+		// here pop the register GYRO & ACCEL configuration and issue a reset so that chip goes to sleep mode (the default one after a reset)
+		mutex_lock(&data->bmi323.mutex);
+
+		// this was done already in runtime_sleep function.
+		if ((data->bmi323.flags & BMI323_FLAGS_RESET_FAILED) != 0x00U) {
+			ret = bmi323_chip_rst(&data->bmi323);
+			if (ret == 0) {
+				data->bmi323.flags &=
+					~BMI323_FLAGS_RESET_FAILED;
+			} else {
+				goto bmi323_bmc150_accel_resume_terminate;
+			}
+		}
+
+		ret = bmi323_write_u16(&data->bmi323,
+				       BMC150_BMI323_FIFO_CONF_REG,
+				       data->bmi323.fifo_conf_reg_value);
+		if (ret != 0) {
+			goto bmi323_bmc150_accel_resume_terminate;
+		}
+
+		ret = bmi323_write_u16(&data->bmi323,
+				       BMC150_BMI323_GYR_CONF_REG,
+				       data->bmi323.gyr_conf_reg_value);
+		if (ret != 0) {
+			goto bmi323_bmc150_accel_resume_terminate;
+		}
+
+		ret = bmi323_write_u16(&data->bmi323,
+				       BMC150_BMI323_ACC_CONF_REG,
+				       data->bmi323.acc_conf_reg_value);
+		if (ret != 0) {
+			goto bmi323_bmc150_accel_resume_terminate;
+		}
+
+bmi323_bmc150_accel_resume_terminate:
+		mutex_unlock(&data->bmi323.mutex);
+		if (ret != 0) {
+			return -EAGAIN;
+		}
+
+		/*
+		 * datasheet says "Start-up time": suspend to high performance mode is tipically 30ms,
+		 * however when setting this to 32 or even higher the first reading from the gyro (unlike accel part)
+		 * is actually the (wrong) default value 0x8000 so it is better to sleep a bit longer
+		 * to prevent issues and give time to the sensor to pick up first readings...
+		 */
+		msleep_interruptible(64);
+
+		return 0;
+	}
+
 	mutex_lock(&data->mutex);
 	bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
 	bmc150_accel_fifo_set_mode(data);
@@ -1863,6 +4081,25 @@ static int bmc150_accel_runtime_suspend(struct device *dev)
 	struct bmc150_accel_data *data = iio_priv(indio_dev);
 	int ret;
 
+	if (data->dev_type == BMI323) {
+		//dev_warn(dev, "bmi323 suspending runtime...");
+
+		/*
+		 * Every operation requiring this function have the mutex locked already:
+		 * with mutex_lock(&data->bmi323.mutex);
+		 */
+		ret = bmi323_chip_rst(&data->bmi323);
+		if (ret != 0) {
+			dev_err(dev,
+				"bmi323 error in runtime_suspend on bmi323_chip_rst: %d\n",
+				ret);
+			data->bmi323.flags |= BMI323_FLAGS_RESET_FAILED;
+			return -EAGAIN;
+		}
+
+		return 0;
+	}
+
 	ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0);
 	if (ret < 0)
 		return -EAGAIN;
@@ -1877,6 +4114,70 @@ static int bmc150_accel_runtime_resume(struct device *dev)
 	int ret;
 	int sleep_val;
 
+	if (data->dev_type == BMI323) {
+		//dev_warn(dev, "bmi323 resuming runtime...");
+
+		/*
+		 * Every operation requiring this function have the mutex locked already:
+		 * with mutex_lock(&data->bmi323.mutex);
+		 */
+
+		// recover from a bad state if it was left that way on reuntime_suspend
+		if ((data->bmi323.flags & BMI323_FLAGS_RESET_FAILED) != 0x00U) {
+			ret = bmi323_chip_rst(&data->bmi323);
+			if (ret == 0) {
+				data->bmi323.flags &=
+					~BMI323_FLAGS_RESET_FAILED;
+			} else {
+				goto bmi323_bmc150_accel_runtime_resume_terminate;
+			}
+		}
+
+		ret = bmi323_write_u16(&data->bmi323,
+				       BMC150_BMI323_FIFO_CONF_REG,
+				       data->bmi323.fifo_conf_reg_value);
+		if (ret != 0) {
+			dev_err(dev,
+				"bmi323 writing to GYR_CONF register failed");
+			goto bmi323_bmc150_accel_runtime_resume_terminate;
+		}
+
+		ret = bmi323_write_u16(&data->bmi323,
+				       BMC150_BMI323_GYR_CONF_REG,
+				       data->bmi323.gyr_conf_reg_value);
+		if (ret != 0) {
+			dev_err(dev,
+				"bmi323 writing to GYR_CONF register failed");
+			goto bmi323_bmc150_accel_runtime_resume_terminate;
+		}
+
+		ret = bmi323_write_u16(&data->bmi323,
+				       BMC150_BMI323_ACC_CONF_REG,
+				       data->bmi323.acc_conf_reg_value);
+		if (ret != 0) {
+			dev_err(dev,
+				"bmi323 writing to ACC_CONF register failed");
+			goto bmi323_bmc150_accel_runtime_resume_terminate;
+		}
+
+bmi323_bmc150_accel_runtime_resume_terminate:
+		if (ret != 0) {
+			dev_err(dev,
+				"bmi323 bmc150_accel_runtime_resume -EAGAIN");
+			return -EAGAIN;
+		}
+
+		/*
+		 * datasheet says "Start-up time": suspend to high performance mode is tipically 30ms,
+		 * however when setting this to 32 or even higher the first reading from the gyro (unlike accel part)
+		 * is actually the (wrong) default value 0x8000 so it is better to sleep a bit longer
+		 * to prevent issues and give time to the sensor to pick up first readings...
+		 */
+		msleep_interruptible(64);
+
+		return 0;
+	}
+
 	ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
 	if (ret < 0)
 		return ret;
diff --git a/drivers/iio/accel/bmc150-accel-i2c.c b/drivers/iio/accel/bmc150-accel-i2c.c
index ee1ba134ad42..0d6ee304b3e7 100644
--- a/drivers/iio/accel/bmc150-accel-i2c.c
+++ b/drivers/iio/accel/bmc150-accel-i2c.c
@@ -173,15 +173,102 @@ static void bmc150_acpi_dual_accel_remove(struct i2c_client *client) {}
 
 static int bmc150_accel_probe(struct i2c_client *client)
 {
+	int ret;
+	u8 chip_id_first[4] = { 0x00, 0x00, 0x00, 0x00 };
+	enum bmc150_device_type dev_type = BMC150;
 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
 	struct regmap *regmap;
 	const char *name = NULL;
 	enum bmc150_type type = BOSCH_UNKNOWN;
+
+	/* reads 4 bytes (2 dummy + 2 good) from the i2c CHIP_ID device register */
+	ret = i2c_smbus_read_i2c_block_data(client, 0x00, 4, &chip_id_first[0]);
+	if (ret != 4) {
+		dev_info(
+			&client->dev,
+			"error checking if the bmc150 is in fact a bmi323: i2c_smbus_read_i2c_block_data = %d: reg = 0x%02x.\n\tIt probably is a bmc150 as correctly reported by the ACPI entry.",
+			(int)ret, 0x00);
+		goto bmi150_old_probe;
+	}
+
+	// at this point we have enough data to know what chip we are handling
+	dev_type = (chip_id_first[2] == 0x43) ? BMI323 : dev_type;
+
+	if (dev_type == BMI323) {
+		dev_warn(
+			&client->dev,
+			"bmc323: what the ACPI table reported as a bmc150 is in fact a bmc323\n");
+
+		struct iio_dev *indio_dev = devm_iio_device_alloc(
+			&client->dev, sizeof(struct bmc150_accel_data));
+		if (!indio_dev) {
+			dev_err(&client->dev,
+				"bmc323 init process failed: out of memory\n");
+
+			return -ENOMEM;
+		}
+
+		dev_set_drvdata(&client->dev, indio_dev);
+		struct bmc150_accel_data *data = iio_priv(indio_dev);
+		data->dev_type = dev_type;
+
+		struct bmi323_private_data *bmi323_data = &data->bmi323;
+		bmi323_data->i2c_client = client;
+		bmi323_data->spi_client = NULL;
+		bmi323_data->irq = client->irq;
+
+		/*
+		 * VDD   is the analog and digital domain voltage supply
+		 * VDDIO is the digital I/O voltage supply
+		 */
+		bmi323_data->regulators[0].supply = "vdd";
+		bmi323_data->regulators[1].supply = "vddio";
+		ret = devm_regulator_bulk_get(
+			&client->dev, ARRAY_SIZE(bmi323_data->regulators),
+			bmi323_data->regulators);
+		if (ret) {
+			return dev_err_probe(&client->dev, ret,
+					     "failed to get regulators\n");
+		}
+
+		ret = regulator_bulk_enable(ARRAY_SIZE(bmi323_data->regulators),
+					    bmi323_data->regulators);
+		if (ret) {
+			iio_device_free(indio_dev);
+
+			dev_err(&client->dev,
+				"failed to enable regulators: %d\n", ret);
+			return ret;
+		}
+
+		ret = bmi323_chip_rst(bmi323_data);
+		if (ret != 0) {
+			dev_err(&client->dev,
+				"bmc323: error issuing the chip reset: %d\n",
+				ret);
+			return ret;
+		}
+
+		dev_info(
+			&client->dev,
+			"bmc323: chip reset success: starting the iio subsystem binding\n");
+
+		ret = bmi323_iio_init(indio_dev);
+		if (ret != 0) {
+			return ret;
+		}
+
+		return 0;
+	}
+
+bmi150_old_probe:
+	dev_info(&client->dev,
+		 "executing the normal procedure for a bmc150...");
+
 	bool block_supported =
 		i2c_check_functionality(client->adapter, I2C_FUNC_I2C) ||
 		i2c_check_functionality(client->adapter,
 					I2C_FUNC_SMBUS_READ_I2C_BLOCK);
-	int ret;
 
 	regmap = devm_regmap_init_i2c(client, &bmc150_regmap_conf);
 	if (IS_ERR(regmap)) {
@@ -198,7 +285,7 @@ static int bmc150_accel_probe(struct i2c_client *client)
 				      type, name, block_supported);
 	if (ret)
 		return ret;
-
+			
 	/*
 	 * The !id check avoids recursion when probe() gets called
 	 * for the second client.
@@ -211,6 +298,15 @@ static int bmc150_accel_probe(struct i2c_client *client)
 
 static void bmc150_accel_remove(struct i2c_client *client)
 {
+	struct iio_dev *indio_dev = dev_get_drvdata(&client->dev);
+	struct bmc150_accel_data *data = iio_priv(indio_dev);
+
+	if (data->dev_type == BMI323) {
+		bmi323_iio_deinit(indio_dev);
+
+		return;
+	}
+
 	bmc150_acpi_dual_accel_remove(client);
 
 	bmc150_accel_core_remove(&client->dev);
diff --git a/drivers/iio/accel/bmc150-accel.h b/drivers/iio/accel/bmc150-accel.h
index 7775c5edaeef..65ec208960df 100644
--- a/drivers/iio/accel/bmc150-accel.h
+++ b/drivers/iio/accel/bmc150-accel.h
@@ -8,6 +8,14 @@
 #include <linux/regulator/consumer.h>
 #include <linux/workqueue.h>
 
+/*
+ * the bmi323 needs raw access to spi and i2c: I cannot use regmap
+ * as this device expects i2c writes to be 2 bytes,
+ * spi reads to be 3 bytes and i2c reads to be 4 bytes.
+ */
+#include <linux/i2c.h>
+#include <linux/spi/spi.h>
+
 struct regmap;
 struct i2c_client;
 struct bmc150_accel_chip_info;
@@ -34,6 +42,11 @@ struct bmc150_accel_interrupt {
 	atomic_t users;
 };
 
+enum bmc150_device_type {
+	BMC150,
+	BMI323,
+};
+
 struct bmc150_accel_trigger {
 	struct bmc150_accel_data *data;
 	struct iio_trigger *indio_trig;
@@ -55,6 +68,25 @@ enum bmc150_accel_trigger_id {
 	BMC150_ACCEL_TRIGGERS,
 };
 
+#define BMI323_FLAGS_RESET_FAILED 0x00000001U
+
+struct bmi323_private_data {
+	struct regulator_bulk_data regulators[2];
+	struct i2c_client *i2c_client;
+	struct spi_device *spi_client;
+	struct device *dev; /* pointer at i2c_client->dev or spi_client->dev */
+	struct mutex mutex;
+	int irq;
+	u32 flags;
+	u16 acc_conf_reg_value;
+	u16 gyr_conf_reg_value;
+	u16 fifo_conf_reg_value;
+	struct iio_trigger *trig[1];
+	s64 fifo_frame_time_diff_ns;
+	s64 acc_odr_time_ns;
+	s64 gyr_odr_time_ns;
+};
+
 struct bmc150_accel_data {
 	struct regmap *regmap;
 	struct regulator_bulk_data regulators[2];
@@ -83,7 +115,67 @@ struct bmc150_accel_data {
 	void (*resume_callback)(struct device *dev);
 	struct delayed_work resume_work;
 	struct iio_mount_matrix orientation;
-};
+	enum bmc150_device_type dev_type;
+	struct bmi323_private_data bmi323;
+	};
+
+/**
+ * This function performs a write of a u16 little-endian (regardless of CPU architecture) integer
+ * to a device register. Returns 0 on success or an error code otherwise.
+ *
+ * PRE: in_value holds the data to be sent to the sensor, in little endian format even on big endian
+ *      architectures.
+ *
+ * NOTE: bmi323->dev can be NULL (not yet initialized) when this function is called
+ *			therefore it is not needed and is not used inside the function
+ *
+ * WARNING: this function does not lock any mutex and synchronization MUST be performed by the caller
+ */
+int bmi323_write_u16(struct bmi323_private_data *bmi323, u8 in_reg, u16 in_value);
+
+/**
+ * This function performs a read of "good" values from the bmi323 discarding what
+ * in the datasheet is described as "dummy data": additional useles bytes.
+ *
+ * PRE: bmi323 has been partially initialized: i2c_device and spi_devices MUST be set to either
+ *      the correct value or NULL
+ *
+ * NOTE: bmi323->dev can be NULL (not yet initialized) when this function is called
+ *			therefore it is not needed and is not used inside the function
+ *
+ * POST: on success out_value is written with data from the sensor, as it came out, so the
+ *       content is little-endian even on big endian architectures
+ *
+ * WARNING: this function does not lock any mutex and synchronization MUST be performed by the caller
+ */
+int bmi323_read_u16(struct bmi323_private_data *bmi323, u8 in_reg, u16* out_value);
+
+int bmi323_chip_check(struct bmi323_private_data *bmi323);
+
+/**
+ * Reset the chip in a known state that is ready to accept commands, but is not configured therefore after calling this function
+ * it is required to load a new configuration to start data acquisition.
+ *
+ * PRE: bmi323 has been fully identified and partially initialized
+ *
+ * NOTE: after issuing a reset the the chip will be in what it is called "suspended mode" and the feature angine is
+ * ready to be set. This mode has everything disabled and consumes aroud 15uA.
+ *
+ * When removing the driver or suspend has been requested it's best to reset the chip so that power consumption
+ * will be the lowest possible.
+ */
+int bmi323_chip_rst(struct bmi323_private_data *bmi323);
+
+/**
+ * This function MUST be called in probe and is responsible for registering the userspace sysfs.
+ *
+ * The indio_dev MUST have been allocated but not registered. This function will perform userspace registration.
+ *
+ * @param indio_dev the industrual io device already allocated but not yet registered
+ */
+int bmi323_iio_init(struct iio_dev *indio_dev);
+
+void bmi323_iio_deinit(struct iio_dev *indio_dev);
 
 int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq,
 			    enum bmc150_type type, const char *name,
-- 
2.42.0