forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_max32.c
1012 lines (873 loc) · 29.4 KB
/
i2c_max32.c
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
/*
* Copyright (c) 2024 Analog Devices, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT adi_max32_i2c
#include <errno.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/drivers/clock_control/adi_max32_clock_control.h>
#include <zephyr/irq.h>
#if defined(CONFIG_I2C_MAX32_DMA)
#include <zephyr/drivers/dma.h>
#endif /* CONFIG_I2C_MAX32_DMA */
#include <wrap_max32_i2c.h>
#define ADI_MAX32_I2C_INT_FL0_MASK 0x00FFFFFF
#define ADI_MAX32_I2C_INT_FL1_MASK 0x7
#define ADI_MAX32_I2C_STATUS_MASTER_BUSY BIT(5)
#define I2C_RECOVER_MAX_RETRIES 3
#ifdef CONFIG_I2C_MAX32_DMA
struct max32_i2c_dma_config {
const struct device *dev;
const uint32_t channel;
const uint32_t slot;
};
#endif /* CONFIG_I2C_MAX32_DMA */
/* Driver config */
struct max32_i2c_config {
mxc_i2c_regs_t *regs;
const struct pinctrl_dev_config *pctrl;
const struct device *clock;
struct max32_perclk perclk;
uint32_t bitrate;
#if defined(CONFIG_I2C_TARGET) || defined(CONFIG_I2C_MAX32_INTERRUPT)
uint8_t irqn;
void (*irq_config_func)(const struct device *dev);
#endif
#ifdef CONFIG_I2C_MAX32_DMA
struct max32_i2c_dma_config tx_dma;
struct max32_i2c_dma_config rx_dma;
#endif /* CONFIG_I2C_MAX32_DMA */
};
struct max32_i2c_data {
mxc_i2c_req_t req;
const struct device *dev;
struct k_sem lock;
uint8_t target_mode;
uint8_t flags;
#ifdef CONFIG_I2C_TARGET
struct i2c_target_config *target_cfg;
bool first_write;
#endif /* CONFIG_I2C_TARGET */
uint32_t readb;
uint32_t written;
#if defined(CONFIG_I2C_MAX32_INTERRUPT) || defined(CONFIG_I2C_MAX32_DMA)
struct k_sem xfer;
int err;
#endif
};
static int api_configure(const struct device *dev, uint32_t dev_cfg)
{
int ret = 0;
const struct max32_i2c_config *const cfg = dev->config;
mxc_i2c_regs_t *i2c = cfg->regs;
switch (I2C_SPEED_GET(dev_cfg)) {
case I2C_SPEED_STANDARD: /** I2C Standard Speed: 100 kHz */
ret = MXC_I2C_SetFrequency(i2c, MXC_I2C_STD_MODE);
break;
case I2C_SPEED_FAST: /** I2C Fast Speed: 400 kHz */
ret = MXC_I2C_SetFrequency(i2c, MXC_I2C_FAST_SPEED);
break;
#if defined(MXC_I2C_FASTPLUS_SPEED)
case I2C_SPEED_FAST_PLUS: /** I2C Fast Plus Speed: 1 MHz */
ret = MXC_I2C_SetFrequency(i2c, MXC_I2C_FASTPLUS_SPEED);
break;
#endif
#if defined(MXC_I2C_HIGH_SPEED)
case I2C_SPEED_HIGH: /** I2C High Speed: 3.4 MHz */
ret = MXC_I2C_SetFrequency(i2c, MXC_I2C_HIGH_SPEED);
break;
#endif
default:
/* Speed not supported */
return -ENOTSUP;
}
return ((ret > 0) ? 0 : -EIO);
}
#ifdef CONFIG_I2C_TARGET
static int api_target_register(const struct device *dev, struct i2c_target_config *cfg)
{
const struct max32_i2c_config *config = dev->config;
struct max32_i2c_data *data = dev->data;
mxc_i2c_regs_t *i2c = config->regs;
int ret;
data->target_cfg = cfg;
ret = MXC_I2C_Init(i2c, 0, cfg->address);
if (ret == E_NO_ERROR) {
data->target_mode = 1;
irq_enable(config->irqn);
MXC_I2C_SlaveTransactionAsync(i2c, NULL);
}
return ret == E_NO_ERROR ? 0 : E_FAIL;
}
static int api_target_unregister(const struct device *dev, struct i2c_target_config *cfg)
{
const struct max32_i2c_config *config = dev->config;
struct max32_i2c_data *data = dev->data;
mxc_i2c_regs_t *i2c = config->regs;
data->target_cfg = NULL;
data->target_mode = 0;
#ifndef CONFIG_I2C_MAX32_INTERRUPT
irq_disable(config->irqn);
#endif
return MXC_I2C_Init(i2c, 1, 0);
}
static int i2c_max32_target_callback(const struct device *dev, mxc_i2c_regs_t *i2c,
mxc_i2c_slave_event_t event)
{
struct max32_i2c_data *data = dev->data;
const struct i2c_target_callbacks *target_cb = data->target_cfg->callbacks;
static uint8_t rxval, txval, rxcnt;
switch (event) {
case MXC_I2C_EVT_MASTER_WR:
if (data->first_write && target_cb->write_requested) {
target_cb->write_requested(data->target_cfg);
data->first_write = false;
}
break;
case MXC_I2C_EVT_MASTER_RD:
break;
case MXC_I2C_EVT_RX_THRESH:
case MXC_I2C_EVT_OVERFLOW:
rxcnt = MXC_I2C_GetRXFIFOAvailable(i2c);
if (target_cb->write_received) {
while (rxcnt--) {
MXC_I2C_ReadRXFIFO(i2c, &rxval, 1);
target_cb->write_received(data->target_cfg, rxval);
}
} else {
MXC_I2C_ClearRXFIFO(i2c);
}
break;
case MXC_I2C_EVT_TX_THRESH:
case MXC_I2C_EVT_UNDERFLOW:
if (target_cb->read_requested) {
target_cb->read_requested(data->target_cfg, &txval);
MXC_I2C_WriteTXFIFO(i2c, &txval, 1);
}
if (target_cb->read_processed) {
target_cb->read_processed(data->target_cfg, &txval);
}
break;
case MXC_I2C_EVT_TRANS_COMP:
if (target_cb->stop) {
target_cb->stop(data->target_cfg);
}
data->first_write = true;
break;
}
return 0;
}
#endif /* CONFIG_I2C_TARGET */
static int api_recover_bus(const struct device *dev)
{
int ret;
const struct max32_i2c_config *const cfg = dev->config;
mxc_i2c_regs_t *i2c = cfg->regs;
ret = MXC_I2C_Recover(i2c, I2C_RECOVER_MAX_RETRIES);
return ret;
}
#ifndef CONFIG_I2C_MAX32_INTERRUPT
static int i2c_max32_transfer_sync(mxc_i2c_regs_t *i2c, struct max32_i2c_data *data)
{
uint32_t int_fl0, int_fl1;
uint32_t readb = 0;
mxc_i2c_req_t *req = &data->req;
/* Wait for acknowledge */
if (data->flags & (I2C_MSG_RESTART | I2C_MSG_READ)) {
do {
MXC_I2C_GetFlags(i2c, &int_fl0, &int_fl1);
} while (!(int_fl0 & ADI_MAX32_I2C_INT_FL0_ADDR_ACK) &&
!(int_fl0 & ADI_MAX32_I2C_INT_FL0_ERR));
}
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_ERR) {
return -EIO;
}
while (req->tx_len > data->written) {
MXC_I2C_GetFlags(i2c, &int_fl0, &int_fl1);
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_TX_THD) {
data->written += MXC_I2C_WriteTXFIFO(i2c, &req->tx_buf[data->written],
req->tx_len - data->written);
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_TX_THD, 0);
}
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_ERR) {
return -EIO;
}
}
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_DONE, 0);
Wrap_MXC_I2C_SetRxCount(i2c, req->rx_len);
while (req->rx_len > readb) {
MXC_I2C_GetFlags(i2c, &int_fl0, &int_fl1);
if (int_fl0 & (ADI_MAX32_I2C_INT_FL0_RX_THD | ADI_MAX32_I2C_INT_FL0_DONE)) {
readb += MXC_I2C_ReadRXFIFO(i2c, &req->rx_buf[readb], req->rx_len - readb);
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_RX_THD, 0);
}
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_ERR) {
return -EIO;
}
MXC_I2C_GetFlags(i2c, &int_fl0, &int_fl1);
if ((int_fl0 & ADI_MAX32_I2C_INT_FL0_DONE) && (req->rx_len > readb) &&
MXC_I2C_GetRXFIFOAvailable(i2c) == 0) {
Wrap_MXC_I2C_SetRxCount(i2c, req->rx_len - readb);
Wrap_MXC_I2C_Restart(i2c);
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_DONE, 0);
i2c->fifo = (req->addr << 1) | 0x1;
}
}
MXC_I2C_GetFlags(i2c, &int_fl0, &int_fl1);
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_ERR) {
return -EIO;
}
if (data->flags & I2C_MSG_STOP) {
MXC_I2C_Stop(i2c);
do {
MXC_I2C_GetFlags(i2c, &int_fl0, &int_fl1);
} while (!(int_fl0 & ADI_MAX32_I2C_INT_FL0_STOP));
}
if (req->rx_len) {
do {
MXC_I2C_GetFlags(i2c, &int_fl0, &int_fl1);
} while (!(int_fl0 & ADI_MAX32_I2C_INT_FL0_DONE));
} else {
while (Wrap_MXC_I2C_GetTxFIFOLevel(i2c) > 0) {
MXC_I2C_GetFlags(i2c, &int_fl0, &int_fl1);
}
}
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_MASK, ADI_MAX32_I2C_INT_FL1_MASK);
return 0;
}
#endif /* CONFIG_I2C_MAX32_INTERRUPT */
#if defined(CONFIG_I2C_MAX32_DMA)
static void i2c_max32_dma_callback(const struct device *dev, void *arg, uint32_t channel,
int status)
{
struct max32_i2c_data *data = arg;
const struct device *i2c_dev = data->dev;
const struct max32_i2c_config *const cfg = i2c_dev->config;
if (status < 0) {
data->err = -EIO;
Wrap_MXC_I2C_SetIntEn(cfg->regs, 0, 0);
k_sem_give(&data->xfer);
} else {
if (data->flags & I2C_MSG_STOP) {
Wrap_MXC_I2C_Stop(cfg->regs);
} else if (!(data->flags & I2C_MSG_READ)) {
MXC_I2C_EnableInt(cfg->regs, ADI_MAX32_I2C_INT_EN0_TX_THD, 0);
}
}
}
static int i2c_max32_tx_dma_load(const struct device *dev, struct i2c_msg *msg)
{
int ret;
const struct max32_i2c_config *config = dev->config;
struct max32_i2c_data *data = dev->data;
struct dma_config dma_cfg = {0};
struct dma_block_config dma_blk = {0};
dma_cfg.channel_direction = MEMORY_TO_PERIPHERAL;
dma_cfg.dma_callback = i2c_max32_dma_callback;
dma_cfg.user_data = (void *)data;
dma_cfg.dma_slot = config->tx_dma.slot;
dma_cfg.block_count = 1;
dma_cfg.source_data_size = 1U;
dma_cfg.source_burst_length = 1U;
dma_cfg.dest_data_size = 1U;
dma_cfg.head_block = &dma_blk;
dma_blk.block_size = msg->len;
dma_blk.source_addr_adj = DMA_ADDR_ADJ_INCREMENT;
dma_blk.source_address = (uint32_t)msg->buf;
ret = dma_config(config->tx_dma.dev, config->tx_dma.channel, &dma_cfg);
if (ret < 0) {
return ret;
}
return dma_start(config->tx_dma.dev, config->tx_dma.channel);
}
static int i2c_max32_rx_dma_load(const struct device *dev, struct i2c_msg *msg)
{
int ret;
const struct max32_i2c_config *config = dev->config;
struct max32_i2c_data *data = dev->data;
struct dma_config dma_cfg = {0};
struct dma_block_config dma_blk = {0};
dma_cfg.channel_direction = PERIPHERAL_TO_MEMORY;
dma_cfg.dma_callback = i2c_max32_dma_callback;
dma_cfg.user_data = (void *)data;
dma_cfg.dma_slot = config->rx_dma.slot;
dma_cfg.block_count = 1;
dma_cfg.source_data_size = 1U;
dma_cfg.source_burst_length = 1U;
dma_cfg.dest_data_size = 1U;
dma_cfg.head_block = &dma_blk;
dma_blk.block_size = msg->len;
dma_blk.dest_addr_adj = DMA_ADDR_ADJ_INCREMENT;
dma_blk.dest_address = (uint32_t)msg->buf;
ret = dma_config(config->rx_dma.dev, config->rx_dma.channel, &dma_cfg);
if (ret < 0) {
return ret;
}
return dma_start(config->rx_dma.dev, config->rx_dma.channel);
}
static int i2c_max32_transfer_dma(const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs,
uint16_t target_address)
{
int ret = 0;
const struct max32_i2c_config *const cfg = dev->config;
struct max32_i2c_data *data = dev->data;
mxc_i2c_regs_t *i2c = cfg->regs;
mxc_i2c_req_t *req = &data->req;
uint8_t target_rw;
unsigned int i = 0;
k_sem_take(&data->lock, K_FOREVER);
req->addr = target_address;
req->i2c = i2c;
MXC_I2C_SetRXThreshold(i2c, 1);
MXC_I2C_SetTXThreshold(i2c, 2);
MXC_I2C_ClearTXFIFO(i2c);
MXC_I2C_ClearRXFIFO(i2c);
/* First message should always begin with a START condition */
msgs[0].flags |= I2C_MSG_RESTART;
for (i = 0; i < num_msgs; i++) {
if (msgs[i].flags & I2C_MSG_READ) {
req->rx_len = msgs[i].len;
req->tx_len = 0;
target_rw = (target_address << 1) | 0x1;
ret = i2c_max32_rx_dma_load(dev, &msgs[i]);
if (ret < 0) {
break;
}
} else {
req->tx_len = msgs[i].len;
req->rx_len = 0;
target_rw = (target_address << 1) & ~0x1;
ret = i2c_max32_tx_dma_load(dev, &msgs[i]);
if (ret < 0) {
break;
}
}
/*
* If previous message ends with a STOP condition, this message
* should begin with a START
*/
if (i > 0) {
if ((msgs[i - 1].flags & (I2C_MSG_STOP | I2C_MSG_READ))) {
msgs[i].flags |= I2C_MSG_RESTART;
}
}
data->flags = msgs[i].flags;
data->readb = 0;
data->written = 0;
data->err = 0;
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_MASK, ADI_MAX32_I2C_INT_FL1_MASK);
MXC_I2C_EnableInt(i2c, ADI_MAX32_I2C_INT_EN0_ERR, 0);
Wrap_MXC_I2C_SetRxCount(i2c, req->rx_len);
if ((data->flags & I2C_MSG_RESTART)) {
MXC_I2C_EnableInt(i2c, ADI_MAX32_I2C_INT_EN0_ADDR_ACK, 0);
MXC_I2C_Start(i2c);
Wrap_MXC_I2C_WaitForRestart(i2c);
MXC_I2C_WriteTXFIFO(i2c, &target_rw, 1);
} else if (req->tx_len) {
MXC_I2C_EnableInt(i2c, ADI_MAX32_I2C_INT_EN0_DONE, 0);
i2c->dma |= ADI_MAX32_I2C_DMA_TX_EN;
}
ret = k_sem_take(&data->xfer, K_FOREVER);
i2c->dma &= ~(ADI_MAX32_I2C_DMA_TX_EN);
i2c->dma &= ~(ADI_MAX32_I2C_DMA_RX_EN);
if (data->err) {
ret = data->err;
}
if (ret) {
MXC_I2C_Stop(i2c);
dma_stop(cfg->tx_dma.dev, cfg->tx_dma.channel);
dma_stop(cfg->rx_dma.dev, cfg->rx_dma.channel);
break;
}
}
k_sem_give(&data->lock);
return ret;
}
#endif /* CONFIG_I2C_MAX32_DMA */
#ifdef CONFIG_I2C_MAX32_INTERRUPT
static int i2c_max32_transfer(const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs,
uint16_t target_address)
{
int ret = 0;
const struct max32_i2c_config *const cfg = dev->config;
struct max32_i2c_data *data = dev->data;
mxc_i2c_regs_t *i2c = cfg->regs;
mxc_i2c_req_t *req = &data->req;
uint8_t target_rw;
unsigned int i = 0;
req->i2c = i2c;
req->addr = target_address;
k_sem_take(&data->lock, K_FOREVER);
MXC_I2C_ClearRXFIFO(i2c);
MXC_I2C_ClearTXFIFO(i2c);
MXC_I2C_SetRXThreshold(i2c, 1);
/* First message should always begin with a START condition */
msgs[0].flags |= I2C_MSG_RESTART;
for (i = 0; i < num_msgs; i++) {
if (msgs[i].flags & I2C_MSG_READ) {
req->rx_buf = (unsigned char *)msgs[i].buf;
req->rx_len = msgs[i].len;
req->tx_buf = NULL;
req->tx_len = 0;
target_rw = (target_address << 1) | 0x1;
} else {
req->tx_buf = (unsigned char *)msgs[i].buf;
req->tx_len = msgs[i].len;
req->rx_buf = NULL;
req->rx_len = 0;
target_rw = (target_address << 1) & ~0x1;
}
/*
* If previous message ends with a STOP condition, this message
* should begin with a START
*/
if (i > 0) {
if ((msgs[i - 1].flags & (I2C_MSG_STOP | I2C_MSG_READ))) {
msgs[i].flags |= I2C_MSG_RESTART;
}
}
data->flags = msgs[i].flags;
data->readb = 0;
data->written = 0;
data->err = 0;
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_MASK, ADI_MAX32_I2C_INT_FL1_MASK);
MXC_I2C_EnableInt(i2c, ADI_MAX32_I2C_INT_EN0_ERR, 0);
Wrap_MXC_I2C_SetRxCount(i2c, req->rx_len);
if ((data->flags & I2C_MSG_RESTART)) {
MXC_I2C_EnableInt(i2c, ADI_MAX32_I2C_INT_EN0_ADDR_ACK, 0);
MXC_I2C_Start(i2c);
Wrap_MXC_I2C_WaitForRestart(i2c);
MXC_I2C_WriteTXFIFO(i2c, &target_rw, 1);
} else {
if (req->tx_len) {
data->written = MXC_I2C_WriteTXFIFO(i2c, req->tx_buf, 1);
MXC_I2C_EnableInt(i2c, ADI_MAX32_I2C_INT_EN0_TX_THD, 0);
} else {
MXC_I2C_EnableInt(i2c, ADI_MAX32_I2C_INT_EN0_RX_THD, 0);
}
}
ret = k_sem_take(&data->xfer, K_FOREVER);
if (data->err) {
MXC_I2C_Stop(i2c);
ret = data->err;
} else {
if (data->flags & I2C_MSG_STOP) {
/* Wait for busy flag to be cleared */
while (i2c->status & ADI_MAX32_I2C_STATUS_MASTER_BUSY) {
}
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_MASK,
ADI_MAX32_I2C_INT_FL1_MASK);
}
}
if (ret) {
break;
}
}
k_sem_give(&data->lock);
return ret;
}
#else
static int i2c_max32_transfer(const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs,
uint16_t target_address)
{
int ret = 0;
const struct max32_i2c_config *const cfg = dev->config;
struct max32_i2c_data *data = dev->data;
mxc_i2c_regs_t *i2c = cfg->regs;
mxc_i2c_req_t *req = &data->req;
uint8_t target_rw;
unsigned int i = 0;
req->i2c = i2c;
req->addr = target_address;
k_sem_take(&data->lock, K_FOREVER);
MXC_I2C_ClearRXFIFO(i2c);
/* First message should always begin with a START condition */
msgs[0].flags |= I2C_MSG_RESTART;
for (i = 0; i < num_msgs; i++) {
if (msgs[i].flags & I2C_MSG_READ) {
req->rx_buf = (unsigned char *)msgs[i].buf;
req->rx_len = msgs[i].len;
req->tx_buf = NULL;
req->tx_len = 0;
target_rw = (target_address << 1) | 0x1;
} else {
req->tx_buf = (unsigned char *)msgs[i].buf;
req->tx_len = msgs[i].len;
req->rx_buf = NULL;
req->rx_len = 0;
target_rw = (target_address << 1) & ~0x1;
}
/*
* If previous message ends with a STOP condition, this message
* should begin with a START
*/
if (i > 0) {
if ((msgs[i - 1].flags & (I2C_MSG_STOP | I2C_MSG_READ))) {
msgs[i].flags |= I2C_MSG_RESTART;
}
}
data->flags = msgs[i].flags;
data->readb = 0;
data->written = 0;
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_MASK, ADI_MAX32_I2C_INT_FL1_MASK);
Wrap_MXC_I2C_SetIntEn(i2c, 0, 0);
if (data->flags & I2C_MSG_RESTART) {
MXC_I2C_Start(i2c);
Wrap_MXC_I2C_WaitForRestart(i2c);
MXC_I2C_WriteTXFIFO(i2c, &target_rw, 1);
}
ret = i2c_max32_transfer_sync(i2c, data);
if (ret) {
MXC_I2C_Stop(i2c);
break;
}
}
k_sem_give(&data->lock);
return ret;
}
#endif /* CONFIG_I2C_MAX32_INTERRUPT */
static int api_transfer(const struct device *dev, struct i2c_msg *msgs, uint8_t num_msgs,
uint16_t target_address)
{
#if CONFIG_I2C_MAX32_DMA
const struct max32_i2c_config *cfg = dev->config;
if ((cfg->tx_dma.channel != 0xFF) && (cfg->rx_dma.channel != 0xFF)) {
return i2c_max32_transfer_dma(dev, msgs, num_msgs, target_address);
}
#endif
return i2c_max32_transfer(dev, msgs, num_msgs, target_address);
}
#ifdef CONFIG_I2C_TARGET
static void i2c_max32_isr_target(const struct device *dev, mxc_i2c_regs_t *i2c)
{
uint32_t ctrl;
uint32_t int_fl0;
uint32_t int_fl1;
uint32_t int_en0;
uint32_t int_en1;
ctrl = i2c->ctrl;
Wrap_MXC_I2C_GetIntEn(i2c, &int_en0, &int_en1);
MXC_I2C_GetFlags(i2c, &int_fl0, &int_fl1);
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_MASK, ADI_MAX32_I2C_INT_FL1_MASK);
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_ERR) {
/* Error occurred, notify callback function and end transaction */
i2c_max32_target_callback(dev, i2c, MXC_I2C_EVT_TRANS_COMP);
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_MASK, ADI_MAX32_I2C_INT_FL1_MASK);
MXC_I2C_ClearTXFIFO(i2c);
MXC_I2C_ClearRXFIFO(i2c);
}
/* Check whether data is available if we received an interrupt occurred while receiving */
if (int_en0 & ADI_MAX32_I2C_INT_EN0_RX_THD || int_en1 & ADI_MAX32_I2C_INT_EN1_RX_OVERFLOW) {
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_RX_THD) {
i2c_max32_target_callback(dev, i2c, MXC_I2C_EVT_RX_THRESH);
}
if (int_fl1 & ADI_MAX32_I2C_INT_FL1_RX_OVERFLOW) {
i2c_max32_target_callback(dev, i2c, MXC_I2C_EVT_OVERFLOW);
}
}
/* Check whether TX FIFO needs to be refilled if interrupt ocurred while transmitting */
if (int_en0 & (ADI_MAX32_I2C_INT_EN0_TX_THD | ADI_MAX32_I2C_INT_EN0_TX_LOCK_OUT) ||
int_en1 & ADI_MAX32_I2C_INT_EN1_TX_UNDERFLOW) {
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_TX_THD) {
i2c_max32_target_callback(dev, i2c, MXC_I2C_EVT_TX_THRESH);
}
if (int_fl1 & ADI_MAX32_I2C_INT_EN1_TX_UNDERFLOW) {
i2c_max32_target_callback(dev, i2c, MXC_I2C_EVT_UNDERFLOW);
}
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_TX_LOCK_OUT) {
int_en0 = ADI_MAX32_I2C_INT_EN0_ADDR_MATCH;
int_en1 = 0;
i2c_max32_target_callback(dev, i2c, MXC_I2C_EVT_TRANS_COMP);
}
}
/* Check if transaction completed or restart occurred */
if (int_en0 & ADI_MAX32_I2C_INT_EN0_DONE) {
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_STOP) {
/* Stop/NACK condition occurred, transaction complete */
i2c_max32_target_callback(dev, i2c, MXC_I2C_EVT_TRANS_COMP);
int_en0 = ADI_MAX32_I2C_INT_EN0_ADDR_MATCH;
} else if (int_fl0 & ADI_MAX32_I2C_INT_FL0_DONE) {
/* Restart detected, re-arm address match interrupt */
int_en0 = ADI_MAX32_I2C_INT_EN0_ADDR_MATCH;
}
int_en1 = 0;
}
/* Check for address match interrupt */
if (int_en0 & ADI_MAX32_I2C_INT_EN0_ADDR_MATCH) {
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_ADDR_MATCH) {
/* Address match occurred, prepare for transaction */
if (i2c->ctrl & MXC_F_I2C_CTRL_READ) {
/* Read request received from the master */
i2c_max32_target_callback(dev, i2c, MXC_I2C_EVT_MASTER_RD);
int_en0 = ADI_MAX32_I2C_INT_EN0_TX_THD |
ADI_MAX32_I2C_INT_EN0_TX_LOCK_OUT |
ADI_MAX32_I2C_INT_EN0_DONE | ADI_MAX32_I2C_INT_EN0_ERR;
int_en1 = ADI_MAX32_I2C_INT_EN1_TX_UNDERFLOW;
} else {
/* Write request received from the master */
i2c_max32_target_callback(dev, i2c, MXC_I2C_EVT_MASTER_WR);
int_en0 = ADI_MAX32_I2C_INT_EN0_RX_THD |
ADI_MAX32_I2C_INT_EN0_DONE | ADI_MAX32_I2C_INT_EN0_ERR;
int_en1 = ADI_MAX32_I2C_INT_EN1_RX_OVERFLOW;
}
}
}
Wrap_MXC_I2C_SetIntEn(i2c, int_en0, int_en1);
}
#endif /* CONFIG_I2C_TARGET */
#ifdef CONFIG_I2C_MAX32_INTERRUPT
static void i2c_max32_isr_controller(const struct device *dev, mxc_i2c_regs_t *i2c)
{
struct max32_i2c_data *data = dev->data;
mxc_i2c_req_t *req = &data->req;
uint32_t written, readb;
uint32_t txfifolevel;
uint32_t int_fl0, int_fl1;
uint32_t int_en0, int_en1;
written = data->written;
readb = data->readb;
Wrap_MXC_I2C_GetIntEn(i2c, &int_en0, &int_en1);
MXC_I2C_GetFlags(i2c, &int_fl0, &int_fl1);
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_MASK, ADI_MAX32_I2C_INT_FL1_MASK);
txfifolevel = Wrap_MXC_I2C_GetTxFIFOLevel(i2c);
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_ERR) {
data->err = -EIO;
Wrap_MXC_I2C_SetIntEn(i2c, 0, 0);
k_sem_give(&data->xfer);
return;
}
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_ADDR_ACK) {
MXC_I2C_DisableInt(i2c, ADI_MAX32_I2C_INT_EN0_ADDR_ACK, 0);
if (written < req->tx_len) {
MXC_I2C_EnableInt(i2c, ADI_MAX32_I2C_INT_EN0_TX_THD, 0);
} else if (readb < req->rx_len) {
MXC_I2C_EnableInt(
i2c, ADI_MAX32_I2C_INT_EN0_RX_THD | ADI_MAX32_I2C_INT_EN0_DONE, 0);
}
}
if (req->tx_len &&
(int_fl0 & (ADI_MAX32_I2C_INT_FL0_TX_THD | ADI_MAX32_I2C_INT_FL0_DONE))) {
if (written < req->tx_len) {
written += MXC_I2C_WriteTXFIFO(i2c, &req->tx_buf[written],
req->tx_len - written);
} else {
if (!(int_en0 & ADI_MAX32_I2C_INT_EN0_DONE)) {
/* We are done, stop sending more data */
MXC_I2C_DisableInt(i2c, ADI_MAX32_I2C_INT_EN0_TX_THD, 0);
if (data->flags & I2C_MSG_STOP) {
MXC_I2C_EnableInt(i2c, ADI_MAX32_I2C_INT_EN0_DONE, 0);
/* Done flag is not set if stop/restart is not set */
Wrap_MXC_I2C_Stop(i2c);
} else {
k_sem_give(&data->xfer);
}
}
if ((int_fl0 & ADI_MAX32_I2C_INT_FL0_DONE)) {
MXC_I2C_DisableInt(i2c, ADI_MAX32_I2C_INT_EN0_DONE, 0);
k_sem_give(&data->xfer);
}
}
} else if ((int_fl0 & (ADI_MAX32_I2C_INT_FL0_RX_THD | ADI_MAX32_I2C_INT_FL0_DONE))) {
readb += MXC_I2C_ReadRXFIFO(i2c, &req->rx_buf[readb], req->rx_len - readb);
if (readb == req->rx_len) {
MXC_I2C_DisableInt(i2c, ADI_MAX32_I2C_INT_EN0_RX_THD, 0);
if (data->flags & I2C_MSG_STOP) {
MXC_I2C_DisableInt(i2c, ADI_MAX32_I2C_INT_EN0_DONE, 0);
Wrap_MXC_I2C_Stop(i2c);
k_sem_give(&data->xfer);
} else {
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_DONE) {
MXC_I2C_DisableInt(i2c, ADI_MAX32_I2C_INT_EN0_DONE, 0);
k_sem_give(&data->xfer);
}
}
} else if ((int_en0 & ADI_MAX32_I2C_INT_EN0_DONE) &&
(int_fl0 & ADI_MAX32_I2C_INT_FL0_DONE)) {
MXC_I2C_DisableInt(
i2c, (ADI_MAX32_I2C_INT_EN0_RX_THD | ADI_MAX32_I2C_INT_EN0_DONE),
0);
Wrap_MXC_I2C_SetRxCount(i2c, req->rx_len - readb);
MXC_I2C_EnableInt(i2c, ADI_MAX32_I2C_INT_EN0_ADDR_ACK, 0);
i2c->fifo = (req->addr << 1) | 0x1;
Wrap_MXC_I2C_Restart(i2c);
}
}
data->written = written;
data->readb = readb;
}
#endif /* CONFIG_I2C_MAX32_INTERRUPT */
#ifdef CONFIG_I2C_MAX32_DMA
static void i2c_max32_isr_controller_dma(const struct device *dev, mxc_i2c_regs_t *i2c)
{
struct max32_i2c_data *data = dev->data;
const struct max32_i2c_config *cfg = dev->config;
struct dma_status dma_stat;
uint32_t int_fl0, int_fl1;
uint32_t int_en0, int_en1;
Wrap_MXC_I2C_GetIntEn(i2c, &int_en0, &int_en1);
MXC_I2C_GetFlags(i2c, &int_fl0, &int_fl1);
MXC_I2C_ClearFlags(i2c, ADI_MAX32_I2C_INT_FL0_MASK, ADI_MAX32_I2C_INT_FL1_MASK);
if (int_fl0 & ADI_MAX32_I2C_INT_FL0_ERR) {
data->err = -EIO;
Wrap_MXC_I2C_SetIntEn(i2c, 0, 0);
k_sem_give(&data->xfer);
} else {
/* Run DMA once address is acknowledged */
if ((int_fl0 & ADI_MAX32_I2C_INT_FL0_ADDR_ACK)) {
MXC_I2C_DisableInt(i2c, ADI_MAX32_I2C_INT_EN0_ADDR_ACK, 0);
MXC_I2C_EnableInt(i2c, ADI_MAX32_I2C_INT_EN0_DONE, 0);
if (data->flags & I2C_MSG_READ) {
i2c->dma |= ADI_MAX32_I2C_DMA_RX_EN;
} else {
i2c->dma |= ADI_MAX32_I2C_DMA_TX_EN;
}
} else if ((int_fl0 & ADI_MAX32_I2C_INT_FL0_DONE)) {
MXC_I2C_DisableInt(i2c, ADI_MAX32_I2C_INT_EN0_DONE, 0);
if ((data->flags & I2C_MSG_READ)) {
dma_get_status(cfg->rx_dma.dev, cfg->rx_dma.channel, &dma_stat);
/* Send RESTART if more data is expected */
if (dma_stat.pending_length > 0) {
Wrap_MXC_I2C_SetRxCount(i2c, dma_stat.pending_length);
MXC_I2C_EnableInt(i2c, ADI_MAX32_I2C_INT_EN0_ADDR_ACK, 0);
i2c->fifo = (data->req.addr << 1) | 0x1;
Wrap_MXC_I2C_Restart(i2c);
} else {
k_sem_give(&data->xfer);
}
} else {
k_sem_give(&data->xfer);
}
} else if ((int_fl0 & ADI_MAX32_I2C_INT_FL0_TX_THD)) {
MXC_I2C_DisableInt(
i2c, ADI_MAX32_I2C_INT_EN0_DONE | ADI_MAX32_I2C_INT_EN0_TX_THD, 0);
k_sem_give(&data->xfer);
}
}
}
#endif /* CONFIG_I2C_MAX32_DMA */
#if defined(CONFIG_I2C_TARGET) || defined(CONFIG_I2C_MAX32_INTERRUPT)
static void i2c_max32_isr(const struct device *dev)
{
const struct max32_i2c_config *cfg = dev->config;
struct max32_i2c_data *data = dev->data;
mxc_i2c_regs_t *i2c = cfg->regs;
#ifdef CONFIG_I2C_MAX32_INTERRUPT
if (data->target_mode == 0) {
#ifdef CONFIG_I2C_MAX32_DMA
if ((cfg->tx_dma.channel != 0xFF) && (cfg->rx_dma.channel != 0xFF)) {
i2c_max32_isr_controller_dma(dev, i2c);
return;
}
#endif
i2c_max32_isr_controller(dev, i2c);
return;
}
#endif /* CONFIG_I2C_MAX32_INTERRUPT */
#ifdef CONFIG_I2C_TARGET
if (data->target_mode == 1) {
i2c_max32_isr_target(dev, i2c);
}
#endif
}
#endif /* CONFIG_I2C_TARGET || CONFIG_I2C_MAX32_INTERRUPT */
static DEVICE_API(i2c, api) = {
.configure = api_configure,
.transfer = api_transfer,
#ifdef CONFIG_I2C_TARGET
.target_register = api_target_register,
.target_unregister = api_target_unregister,
#endif
#ifdef CONFIG_I2C_RTIO
.iodev_submit = i2c_iodev_submit_fallback,
#endif
.recover_bus = api_recover_bus,
};
static int i2c_max32_init(const struct device *dev)
{
const struct max32_i2c_config *const cfg = dev->config;
struct max32_i2c_data *data = dev->data;
mxc_i2c_regs_t *i2c = cfg->regs;
int ret = 0;
if (!device_is_ready(cfg->clock)) {
return -ENODEV;
}
MXC_I2C_Shutdown(i2c); /* Clear everything out */
ret = clock_control_on(cfg->clock, (clock_control_subsys_t)&cfg->perclk);
if (ret) {
return ret;
}
ret = pinctrl_apply_state(cfg->pctrl, PINCTRL_STATE_DEFAULT);
if (ret) {
return ret;
}
ret = MXC_I2C_Init(i2c, 1, 0); /* Configure as master */
if (ret) {
return ret;
}
MXC_I2C_SetFrequency(i2c, cfg->bitrate);
k_sem_init(&data->lock, 1, 1);
#if defined(CONFIG_I2C_TARGET) || defined(CONFIG_I2C_MAX32_INTERRUPT)
cfg->irq_config_func(dev);
#endif
#ifdef CONFIG_I2C_MAX32_INTERRUPT
irq_enable(cfg->irqn);
k_sem_init(&data->xfer, 0, 1);
#endif
#if defined(CONFIG_I2C_TARGET)
data->first_write = true;
data->target_mode = 0;
#endif
data->dev = dev;
return ret;
}
#if defined(CONFIG_I2C_TARGET) || defined(CONFIG_I2C_MAX32_INTERRUPT)
#define I2C_MAX32_CONFIG_IRQ_FUNC(n) \
.irq_config_func = i2c_max32_irq_config_func_##n, .irqn = DT_INST_IRQN(n),
#define I2C_MAX32_IRQ_CONFIG_FUNC(n) \
static void i2c_max32_irq_config_func_##n(const struct device *dev) \
{ \
IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), i2c_max32_isr, \
DEVICE_DT_INST_GET(n), 0); \
}
#else
#define I2C_MAX32_CONFIG_IRQ_FUNC(n)
#define I2C_MAX32_IRQ_CONFIG_FUNC(n)
#endif
#if CONFIG_I2C_MAX32_DMA
#define MAX32_DT_INST_DMA_CTLR(n, name) \
COND_CODE_1(DT_INST_NODE_HAS_PROP(n, dmas), \
(DEVICE_DT_GET(DT_INST_DMAS_CTLR_BY_NAME(n, name))), (NULL))
#define MAX32_DT_INST_DMA_CELL(n, name, cell) \
COND_CODE_1(DT_INST_NODE_HAS_PROP(n, dmas), (DT_INST_DMAS_CELL_BY_NAME(n, name, cell)), \
(0xff))
#define MAX32_I2C_TX_DMA_INIT(n) \
.tx_dma.dev = MAX32_DT_INST_DMA_CTLR(n, tx), \
.tx_dma.channel = MAX32_DT_INST_DMA_CELL(n, tx, channel), \
.tx_dma.slot = MAX32_DT_INST_DMA_CELL(n, tx, slot),
#define MAX32_I2C_RX_DMA_INIT(n) \
.rx_dma.dev = MAX32_DT_INST_DMA_CTLR(n, rx), \
.rx_dma.channel = MAX32_DT_INST_DMA_CELL(n, rx, channel), \
.rx_dma.slot = MAX32_DT_INST_DMA_CELL(n, rx, slot),
#else
#define MAX32_I2C_TX_DMA_INIT(n)
#define MAX32_I2C_RX_DMA_INIT(n)
#endif
#define DEFINE_I2C_MAX32(_num) \
PINCTRL_DT_INST_DEFINE(_num); \
I2C_MAX32_IRQ_CONFIG_FUNC(_num) \
static const struct max32_i2c_config max32_i2c_dev_cfg_##_num = { \
.regs = (mxc_i2c_regs_t *)DT_INST_REG_ADDR(_num), \
.pctrl = PINCTRL_DT_INST_DEV_CONFIG_GET(_num), \