Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
/*
 * Copyright (c) 2023 Nuvoton Technology Corporation.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#define DT_DRV_COMPAT nuvoton_numaker_i2c

#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/clock_control.h>
#include <zephyr/drivers/clock_control/clock_control_numaker.h>
#include <zephyr/drivers/reset.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(i2c_numaker, CONFIG_I2C_LOG_LEVEL);

#include "i2c-priv.h"
#include <soc.h>
#include <NuMicro.h>

/* i2c Master Mode Status */
#define  M_START               0x08  /* Start */
#define  M_REPEAT_START        0x10  /* Master Repeat Start */
#define  M_TRAN_ADDR_ACK       0x18  /* Master Transmit Address ACK */
#define  M_TRAN_ADDR_NACK      0x20  /* Master Transmit Address NACK */
#define  M_TRAN_DATA_ACK       0x28  /* Master Transmit Data ACK */
#define  M_TRAN_DATA_NACK      0x30  /* Master Transmit Data NACK */
#define  M_ARB_LOST            0x38  /* Master Arbitration Los */
#define  M_RECE_ADDR_ACK       0x40  /* Master Receive Address ACK */
#define  M_RECE_ADDR_NACK      0x48  /* Master Receive Address NACK */
#define  M_RECE_DATA_ACK       0x50  /* Master Receive Data ACK */
#define  M_RECE_DATA_NACK      0x58  /* Master Receive Data NACK */
#define  BUS_ERROR             0x00  /* Bus error */

/* i2c Slave Mode Status */
#define  S_REPEAT_START_STOP   0xA0  /* Slave Transmit Repeat Start or Stop */
#define  S_TRAN_ADDR_ACK       0xA8  /* Slave Transmit Address ACK */
#define  S_TRAN_DATA_ACK       0xB8  /* Slave Transmit Data ACK */
#define  S_TRAN_DATA_NACK      0xC0  /* Slave Transmit Data NACK */
#define  S_TRAN_LAST_DATA_ACK  0xC8  /* Slave Transmit Last Data ACK */
#define  S_RECE_ADDR_ACK       0x60  /* Slave Receive Address ACK */
#define  S_RECE_ARB_LOST       0x68  /* Slave Receive Arbitration Lost */
#define  S_RECE_DATA_ACK       0x80  /* Slave Receive Data ACK */
#define  S_RECE_DATA_NACK      0x88  /* Slave Receive Data NACK */

/* i2c GC Mode Status */
#define  GC_ADDR_ACK           0x70  /* GC mode Address ACK */
#define  GC_ARB_LOST           0x78  /* GC mode Arbitration Lost */
#define  GC_DATA_ACK           0x90  /* GC mode Data ACK */
#define  GC_DATA_NACK          0x98  /* GC mode Data NACK */

/* i2c Other Status */
#define  ADDR_TRAN_ARB_LOST    0xB0  /* Address Transmit Arbitration Lost */
#define  BUS_RELEASED          0xF8  /* Bus Released */

struct i2c_numaker_config {
	I2C_T *i2c_base;
	const struct reset_dt_spec reset;
	uint32_t clk_modidx;
	uint32_t clk_src;
	uint32_t clk_div;
	const struct device *clkctrl_dev;
	uint32_t irq_n;
	void (*irq_config_func)(const struct device *dev);
	const struct pinctrl_dev_config *pincfg;
	uint32_t bitrate;
};

struct i2c_numaker_data {
	struct k_sem lock;
	uint32_t dev_config;
	/* Master transfer context */
	struct {
		struct k_sem xfer_sync;
		uint16_t addr;
		struct i2c_msg *msgs_beg;
		struct i2c_msg *msgs_pos;
		struct i2c_msg *msgs_end;
		uint8_t *buf_beg;
		uint8_t *buf_pos;
		uint8_t *buf_end;
	} master_xfer;
#ifdef CONFIG_I2C_TARGET
	/* Slave transfer context */
	struct {
		struct i2c_target_config *slave_config;
		bool slave_addressed;
	} slave_xfer;
#endif
};

/* ACK/NACK last data byte, dependent on whether or not message merge is allowed */
static void m_numaker_i2c_master_xfer_msg_read_last_byte(const struct device *dev)
{
	const struct i2c_numaker_config *config = dev->config;
	struct i2c_numaker_data *data = dev->data;
	I2C_T *i2c_base = config->i2c_base;

	/* Shouldn't invoke with message pointer OOB */
	__ASSERT_NO_MSG(data->master_xfer.msgs_pos < data->master_xfer.msgs_end);
	/* Should invoke with exactly one data byte remaining for read */
	__ASSERT_NO_MSG((data->master_xfer.msgs_pos->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ);
	__ASSERT_NO_MSG((data->master_xfer.buf_end - data->master_xfer.buf_pos) == 1);

	/* Flags of previous message */
	bool do_stop_prev = data->master_xfer.msgs_pos->flags & I2C_MSG_STOP;

	/* Advance to next messages temporarily */
	data->master_xfer.msgs_pos++;

	/* Has next message? */
	if (data->master_xfer.msgs_pos < data->master_xfer.msgs_end) {
		/* Flags of next message */
		struct i2c_msg *msgs_pos = data->master_xfer.msgs_pos;
		bool is_read_next = (msgs_pos->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ;
		bool do_restart_next = data->master_xfer.msgs_pos->flags & I2C_MSG_RESTART;

		/*
		 * Different R/W bit so message merge is disallowed.
		 * Force I2C Repeat Start on I2C Stop/Repeat Start missing
		 */
		if (!is_read_next) {
			if (!do_stop_prev && !do_restart_next) {
				do_restart_next = true;
			}
		}

		if (do_stop_prev || do_restart_next) {
			/* NACK last data byte (required for Master Receiver) */
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk);
		} else {
			/* ACK last data byte, so to merge adjacent messages into one transaction */
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk | I2C_CTL0_AA_Msk);
		}
	} else {
		/* NACK last data byte (required for Master Receiver) */
		I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk);
	}

	/* Roll back message pointer */
	data->master_xfer.msgs_pos--;
}

/* End the transfer, involving I2C Stop and signal to thread */
static void m_numaker_i2c_master_xfer_end(const struct device *dev, bool do_stop)
{
	const struct i2c_numaker_config *config = dev->config;
	struct i2c_numaker_data *data = dev->data;
	I2C_T *i2c_base = config->i2c_base;

	if (do_stop) {
		/* Do I2C Stop */
		I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_STO_Msk | I2C_CTL0_SI_Msk);
	}

	/* Signal master transfer end */
	k_sem_give(&data->master_xfer.xfer_sync);
}

static void m_numaker_i2c_master_xfer_msg_end(const struct device *dev);
/* Read next data byte, involving ACK/NACK last data byte and message merge */
static void m_numaker_i2c_master_xfer_msg_read_next_byte(const struct device *dev)
{
	const struct i2c_numaker_config *config = dev->config;
	struct i2c_numaker_data *data = dev->data;
	I2C_T *i2c_base = config->i2c_base;

	switch (data->master_xfer.buf_end - data->master_xfer.buf_pos) {
	case 0:
		/* Last data byte ACKed, we'll do message merge */
		m_numaker_i2c_master_xfer_msg_end(dev);
		break;
	case 1:
		/* Read last data byte for this message */
		m_numaker_i2c_master_xfer_msg_read_last_byte(dev);
		break;
	default:
		/* ACK non-last data byte */
		I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk | I2C_CTL0_AA_Msk);
	}
}

/* End one message transfer, involving message merge and transfer end */
static void m_numaker_i2c_master_xfer_msg_end(const struct device *dev)
{
	const struct i2c_numaker_config *config = dev->config;
	struct i2c_numaker_data *data = dev->data;
	I2C_T *i2c_base = config->i2c_base;

	/* Shouldn't invoke with message pointer OOB */
	__ASSERT_NO_MSG(data->master_xfer.msgs_pos < data->master_xfer.msgs_end);
	/* Should have transferred up */
	__ASSERT_NO_MSG((data->master_xfer.buf_end - data->master_xfer.buf_pos) == 0);

	/* Flags of previous message */
	bool is_read_prev = (data->master_xfer.msgs_pos->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ;
	bool do_stop_prev = data->master_xfer.msgs_pos->flags & I2C_MSG_STOP;

	/* Advance to next messages */
	data->master_xfer.msgs_pos++;

	/* Has next message? */
	if (data->master_xfer.msgs_pos < data->master_xfer.msgs_end) {
		/* Flags of next message */
		struct i2c_msg *msgs_pos = data->master_xfer.msgs_pos;
		bool is_read_next = (msgs_pos->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ;
		bool do_restart_next = data->master_xfer.msgs_pos->flags & I2C_MSG_RESTART;

		/*
		 * Different R/W bit so message merge is disallowed.
		 * Force I2C Repeat Start on I2C Stop/Repeat Start missing
		 */
		if (!is_read_prev != !is_read_next) {   /* Logical XOR idiom */
			if (!do_stop_prev && !do_restart_next) {
				LOG_WRN("Cannot merge adjacent messages, force I2C Repeat Start");
				do_restart_next = true;
			}
		}

		if (do_stop_prev) {
			/* Do I2C Stop and then Start */
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_STA_Msk |
					    I2C_CTL0_STO_Msk | I2C_CTL0_SI_Msk);
		} else if (do_restart_next) {
			/* Do I2C Repeat Start */
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_STA_Msk | I2C_CTL0_SI_Msk);
		} else {
			/* Merge into the same transaction */

			/* Prepare buffer for current message */
			data->master_xfer.buf_beg = data->master_xfer.msgs_pos->buf;
			data->master_xfer.buf_pos = data->master_xfer.msgs_pos->buf;
			data->master_xfer.buf_end = data->master_xfer.msgs_pos->buf +
						    data->master_xfer.msgs_pos->len;

			if (is_read_prev) {
				m_numaker_i2c_master_xfer_msg_read_next_byte(dev);
			} else {
				/*
				 * Interrupt flag not cleared, expect to re-enter ISR with
				 * context unchanged, except buffer changed for message change.
				 */
			}
		}
	} else {
		if (!do_stop_prev) {
			LOG_WRN("Last message not marked I2C Stop");
		}

		m_numaker_i2c_master_xfer_end(dev, do_stop_prev);
	}
}

static int i2c_numaker_configure(const struct device *dev, uint32_t dev_config)
{
	const struct i2c_numaker_config *config = dev->config;
	struct i2c_numaker_data *data = dev->data;
	uint32_t bitrate;

	/* Check address size */
	if (dev_config & I2C_ADDR_10_BITS) {
		LOG_ERR("10-bits address not supported");
		return -ENOTSUP;
	}

	switch (I2C_SPEED_GET(dev_config)) {
	case I2C_SPEED_STANDARD:
		bitrate = KHZ(100);
		break;
	case I2C_SPEED_FAST:
		bitrate = KHZ(400);
		break;
	case I2C_SPEED_FAST_PLUS:
		bitrate = MHZ(1);
		break;
	default:
		LOG_ERR("Speed code %d not supported", I2C_SPEED_GET(dev_config));
		return -ENOTSUP;
	}

	I2C_T *i2c_base = config->i2c_base;
	int err = 0;

	k_sem_take(&data->lock, K_FOREVER);
	irq_disable(config->irq_n);

#ifdef CONFIG_I2C_TARGET
	if (data->slave_xfer.slave_addressed) {
		LOG_ERR("Reconfigure with slave being busy");
		err = -EBUSY;
		goto done;
	}
#endif

	I2C_Open(i2c_base, bitrate);
	/* INTEN bit and FSM control bits (STA, STO, SI, AA) are packed in one register CTL0. */
	i2c_base->CTL0 |= (I2C_CTL0_INTEN_Msk | I2C_CTL0_I2CEN_Msk);
	data->dev_config = dev_config;

done:

	irq_enable(config->irq_n);
	k_sem_give(&data->lock);

	return err;
}

static int i2c_numaker_get_config(const struct device *dev, uint32_t *dev_config)
{
	struct i2c_numaker_data *data = dev->data;

	if (!dev_config) {
		return -EINVAL;
	}

	k_sem_take(&data->lock, K_FOREVER);
	*dev_config = data->dev_config;
	k_sem_give(&data->lock);

	return 0;
}

/*
 * Master active transfer:
 * 1. Do I2C Start to start the transfer (thread)
 * 2. I2C FSM (ISR)
 * 3. Force I2C Stop to end the transfer (thread)
 * Slave passive transfer:
 * 1. Prepare callback (thread)
 * 2. Do data transfer via above callback (ISR)
 */
static int i2c_numaker_transfer(const struct device *dev, struct i2c_msg *msgs,
				uint8_t num_msgs, uint16_t addr)
{
	const struct i2c_numaker_config *config = dev->config;
	struct i2c_numaker_data *data = dev->data;
	I2C_T *i2c_base = config->i2c_base;
	int err = 0;

	k_sem_take(&data->lock, K_FOREVER);
	irq_disable(config->irq_n);

	if (data->slave_xfer.slave_addressed) {
		LOG_ERR("Master transfer with slave being busy");
		err = -EBUSY;
		goto cleanup;
	}

	if (num_msgs == 0) {
		goto cleanup;
	}

	/* Prepare to start transfer */
	data->master_xfer.addr = addr;
	data->master_xfer.msgs_beg = msgs;
	data->master_xfer.msgs_pos = msgs;
	data->master_xfer.msgs_end = msgs + num_msgs;

	/* Do I2C Start to start the transfer */
	I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_STA_Msk | I2C_CTL0_SI_Msk);

	irq_enable(config->irq_n);
	k_sem_take(&data->master_xfer.xfer_sync, K_FOREVER);
	irq_disable(config->irq_n);

	/* Check transfer result */
	if (data->master_xfer.msgs_pos != data->master_xfer.msgs_end) {
		bool is_read;
		bool is_10bit;

		is_read = (data->master_xfer.msgs_pos->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ;
		is_10bit = data->master_xfer.msgs_pos->flags & I2C_MSG_ADDR_10_BITS;
		LOG_ERR("Failed message:");
		LOG_ERR("MSG IDX: %d", data->master_xfer.msgs_pos - data->master_xfer.msgs_beg);
		LOG_ERR("ADDR (%d-bit): 0x%04X", is_10bit ? 10 : 7, addr);
		LOG_ERR("DIR: %s", is_read ? "R" : "W");
		LOG_ERR("Expected %d bytes transferred, but actual %d",
			data->master_xfer.msgs_pos->len,
			data->master_xfer.buf_pos - data->master_xfer.buf_beg);
		err = -EIO;
		goto i2c_stop;
	}

i2c_stop:

	/* Do I2C Stop to release bus ownership */
	I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_STO_Msk | I2C_CTL0_SI_Msk);

#ifdef CONFIG_I2C_TARGET
	/* Enable slave mode if one slave is registered */
	if (data->slave_xfer.slave_config) {
		I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk | I2C_CTL0_AA_Msk);
	}
#endif

cleanup:

	irq_enable(config->irq_n);
	k_sem_give(&data->lock);

	return err;
}

#ifdef CONFIG_I2C_TARGET
static int i2c_numaker_slave_register(const struct device *dev,
				      struct i2c_target_config *slave_config)
{
	if (!slave_config || !slave_config->callbacks) {
		return -EINVAL;
	}

	if (slave_config->flags & I2C_ADDR_10_BITS) {
		LOG_ERR("10-bits address not supported");
		return -ENOTSUP;
	}

	const struct i2c_numaker_config *config = dev->config;
	struct i2c_numaker_data *data = dev->data;
	I2C_T *i2c_base = config->i2c_base;
	int err = 0;

	k_sem_take(&data->lock, K_FOREVER);
	irq_disable(config->irq_n);

	if (data->slave_xfer.slave_config) {
		err = -EBUSY;
		goto cleanup;
	}

	data->slave_xfer.slave_config = slave_config;
	/* Slave address */
	I2C_SetSlaveAddr(i2c_base,
			 0,
			 slave_config->address,
			 I2C_GCMODE_DISABLE);

	/* Slave address state */
	data->slave_xfer.slave_addressed = false;

	/* Enable slave mode */
	I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk | I2C_CTL0_AA_Msk);

cleanup:

	irq_enable(config->irq_n);
	k_sem_give(&data->lock);

	return err;
}

static int i2c_numaker_slave_unregister(const struct device *dev,
					struct i2c_target_config *slave_config)
{
	const struct i2c_numaker_config *config = dev->config;
	struct i2c_numaker_data *data = dev->data;
	I2C_T *i2c_base = config->i2c_base;
	int err = 0;

	if (!slave_config) {
		return -EINVAL;
	}

	k_sem_take(&data->lock, K_FOREVER);
	irq_disable(config->irq_n);

	if (data->slave_xfer.slave_config != slave_config) {
		err = -EINVAL;
		goto cleanup;
	}

	if (data->slave_xfer.slave_addressed) {
		LOG_ERR("Unregister slave driver with slave being busy");
		err = -EBUSY;
		goto cleanup;
	}

	/* Slave address: Zero */
	I2C_SetSlaveAddr(i2c_base,
			 0,
			 0,
			 I2C_GCMODE_DISABLE);

	/* Slave address state */
	data->slave_xfer.slave_addressed = false;

	/* Disable slave mode */
	I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk);
	data->slave_xfer.slave_config = NULL;

cleanup:

	irq_enable(config->irq_n);
	k_sem_give(&data->lock);

	return err;
}
#endif

static int i2c_numaker_recover_bus(const struct device *dev)
{
	const struct i2c_numaker_config *config = dev->config;
	struct i2c_numaker_data *data = dev->data;
	I2C_T *i2c_base = config->i2c_base;

	k_sem_take(&data->lock, K_FOREVER);
	/* Do I2C Stop to release bus ownership */
	I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_STO_Msk | I2C_CTL0_SI_Msk);
	k_sem_give(&data->lock);

	return 0;
}

static void i2c_numaker_isr(const struct device *dev)
{
	const struct i2c_numaker_config *config = dev->config;
	struct i2c_numaker_data *data = dev->data;
	I2C_T *i2c_base = config->i2c_base;
#ifdef CONFIG_I2C_TARGET
	struct i2c_target_config *slave_config = data->slave_xfer.slave_config;
	const struct i2c_target_callbacks *slave_callbacks =
		slave_config ? slave_config->callbacks : NULL;
	uint8_t data_byte;
#endif
	uint32_t status;

	if (I2C_GET_TIMEOUT_FLAG(i2c_base)) {
		I2C_ClearTimeoutFlag(i2c_base);
		return;
	}

	status = I2C_GET_STATUS(i2c_base);

	switch (status) {
	case M_START: /* Start */
	case M_REPEAT_START: /* Master Repeat Start */
		/* Prepare buffer for current message */
		data->master_xfer.buf_beg = data->master_xfer.msgs_pos->buf;
		data->master_xfer.buf_pos = data->master_xfer.msgs_pos->buf;
		data->master_xfer.buf_end = data->master_xfer.msgs_pos->buf +
					      data->master_xfer.msgs_pos->len;

		/* Write I2C address */
		struct i2c_msg *msgs_pos = data->master_xfer.msgs_pos;
		bool is_read = (msgs_pos->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ;
		uint16_t addr = data->master_xfer.addr;
		int addr_rw = is_read ? ((addr << 1) | 1) : (addr << 1);

		I2C_SET_DATA(i2c_base, (uint8_t) (addr_rw & 0xFF));
		I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk);
		break;
	case M_TRAN_ADDR_ACK: /* Master Transmit Address ACK */
	case M_TRAN_DATA_ACK: /* Master Transmit Data ACK */
		__ASSERT_NO_MSG(data->master_xfer.buf_pos);
		if (data->master_xfer.buf_pos < data->master_xfer.buf_end) {
			I2C_SET_DATA(i2c_base, *data->master_xfer.buf_pos++);
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk | I2C_CTL0_AA_Msk);
		} else {
			/* End this message */
			m_numaker_i2c_master_xfer_msg_end(dev);
		}
		break;
	case M_TRAN_ADDR_NACK:  /* Master Transmit Address NACK */
	case M_TRAN_DATA_NACK:  /* Master Transmit Data NACK */
	case M_RECE_ADDR_NACK:  /* Master Receive Address NACK */
	case M_ARB_LOST:  /* Master Arbitration Lost */
		m_numaker_i2c_master_xfer_end(dev, true);
		break;
	case M_RECE_ADDR_ACK:  /* Master Receive Address ACK */
	case M_RECE_DATA_ACK:  /* Master Receive Data ACK */
		__ASSERT_NO_MSG(data->master_xfer.buf_pos);

		if (status == M_RECE_ADDR_ACK) {
			__ASSERT_NO_MSG(data->master_xfer.buf_pos < data->master_xfer.buf_end);
		} else if (status == M_RECE_DATA_ACK) {
			__ASSERT_NO_MSG((data->master_xfer.buf_end -
					 data->master_xfer.buf_pos) >= 1);
			*data->master_xfer.buf_pos++ = I2C_GET_DATA(i2c_base);
		}

		m_numaker_i2c_master_xfer_msg_read_next_byte(dev);
		break;
	case M_RECE_DATA_NACK:  /* Master Receive Data NACK */
		__ASSERT_NO_MSG((data->master_xfer.buf_end - data->master_xfer.buf_pos) == 1);
		*data->master_xfer.buf_pos++ = I2C_GET_DATA(i2c_base);
		/* End this message */
		m_numaker_i2c_master_xfer_msg_end(dev);
		break;
	case BUS_ERROR:	 /* Bus error */
		m_numaker_i2c_master_xfer_end(dev, true);
		break;
#ifdef CONFIG_I2C_TARGET
	/* NOTE: Don't disable interrupt here because slave mode relies on */
	/* for passive transfer in ISR. */

	/* Slave Transmit */
	case S_TRAN_ADDR_ACK:  /* Slave Transmit Address ACK */
	case ADDR_TRAN_ARB_LOST:  /* Slave Transmit Arbitration Lost */
		data->slave_xfer.slave_addressed = true;
		if (slave_callbacks->read_requested(slave_config, &data_byte) == 0) {
			/* Non-last data byte */
			I2C_SET_DATA(i2c_base, data_byte);
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk | I2C_CTL0_AA_Msk);
		} else {
			/* Go S_TRAN_LAST_DATA_ACK on error */
			I2C_SET_DATA(i2c_base, 0xFF);
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk);
		}
		break;
	case S_TRAN_DATA_ACK:  /* Slave Transmit Data ACK */
		if (slave_callbacks->read_processed(slave_config, &data_byte) == 0) {
			/* Non-last data byte */
			I2C_SET_DATA(i2c_base, data_byte);
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk | I2C_CTL0_AA_Msk);
		} else {
			/* Go S_TRAN_LAST_DATA_ACK on error */
			I2C_SET_DATA(i2c_base, 0xFF);
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk);
		}
		break;
	case S_TRAN_DATA_NACK:  /* Slave Transmit Data NACK */
	case S_TRAN_LAST_DATA_ACK:  /* Slave Transmit Last Data ACK */
		/* Go slave end */
		data->slave_xfer.slave_addressed = false;
		slave_callbacks->stop(slave_config);
		I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk | I2C_CTL0_AA_Msk);
		break;
		/* Slave Receive */
	case S_RECE_DATA_ACK:  /* Slave Receive Data ACK */
		data_byte = I2C_GET_DATA(i2c_base);
		if (slave_callbacks->write_received(slave_config, data_byte) == 0) {
			/* Write OK, ACK next data byte */
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk | I2C_CTL0_AA_Msk);
		} else {
			/* Write FAILED, NACK next data byte */
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk);
		}
		break;
	case S_RECE_DATA_NACK:  /* Slave Receive Data NACK */
		/* Go slave end */
		data->slave_xfer.slave_addressed = false;
		slave_callbacks->stop(slave_config);
		I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk | I2C_CTL0_AA_Msk);
		break;
	case S_RECE_ADDR_ACK:  /* Slave Receive Address ACK */
	case S_RECE_ARB_LOST:  /* Slave Receive Arbitration Lost */
		data->slave_xfer.slave_addressed = true;
		if (slave_callbacks->write_requested(slave_config) == 0) {
			/* Write ready, ACK next byte */
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk | I2C_CTL0_AA_Msk);
		} else {
			/* Write not ready, NACK next byte */
			I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk);
		}
		break;
	case S_REPEAT_START_STOP:  /* Slave Transmit/Receive Repeat Start or Stop */
		/* Go slave end */
		data->slave_xfer.slave_addressed = false;
		slave_callbacks->stop(slave_config);
		I2C_SET_CONTROL_REG(i2c_base, I2C_CTL0_SI_Msk | I2C_CTL0_AA_Msk);
		break;
#endif  /* CONFIG_I2C_TARGET */

	case BUS_RELEASED:  /* Bus Released */
		/* Ignore the interrupt raised by BUS_RELEASED. */
		break;
	default:
		__ASSERT(false, "Uncaught I2C FSM state");
		m_numaker_i2c_master_xfer_end(dev, true);
	}
}

static int i2c_numaker_init(const struct device *dev)
{
	const struct i2c_numaker_config *config = dev->config;
	struct i2c_numaker_data *data = dev->data;
	int err = 0;
	struct numaker_scc_subsys scc_subsys;

	/* Validate this module's reset object */
	if (!device_is_ready(config->reset.dev)) {
		LOG_ERR("reset controller not ready");
		return -ENODEV;
	}

	/* Clean mutable context */
	memset(data, 0x00, sizeof(*data));

	k_sem_init(&data->lock, 1, 1);
	k_sem_init(&data->master_xfer.xfer_sync, 0, 1);

	SYS_UnlockReg();

	memset(&scc_subsys, 0x00, sizeof(scc_subsys));
	scc_subsys.subsys_id = NUMAKER_SCC_SUBSYS_ID_PCC;
	scc_subsys.pcc.clk_modidx = config->clk_modidx;
	scc_subsys.pcc.clk_src = config->clk_src;
	scc_subsys.pcc.clk_div = config->clk_div;

	/* Equivalent to CLK_EnableModuleClock() */
	err = clock_control_on(config->clkctrl_dev, (clock_control_subsys_t) &scc_subsys);
	if (err != 0) {
		goto cleanup;
	}
	/* Equivalent to CLK_SetModuleClock() */
	err = clock_control_configure(config->clkctrl_dev,
				      (clock_control_subsys_t) &scc_subsys,
				      NULL);
	if (err != 0) {
		goto cleanup;
	}

	/* Configure pinmux (NuMaker's SYS MFP) */
	err = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT);
	if (err != 0) {
		goto cleanup;
	}

	/* Reset I2C to default state, same as BSP's SYS_ResetModule(id_rst) */
	reset_line_toggle_dt(&config->reset);

	err = i2c_numaker_configure(dev, I2C_MODE_CONTROLLER | i2c_map_dt_bitrate(config->bitrate));
	if (err != 0) {
		goto cleanup;
	}

	config->irq_config_func(dev);

cleanup:

	SYS_LockReg();
	return err;
}

static const struct i2c_driver_api i2c_numaker_driver_api = {
	.configure = i2c_numaker_configure,
	.get_config = i2c_numaker_get_config,
	.transfer = i2c_numaker_transfer,
#ifdef CONFIG_I2C_TARGET
	.target_register = i2c_numaker_slave_register,
	.target_unregister = i2c_numaker_slave_unregister,
#endif
	.recover_bus = i2c_numaker_recover_bus,
};

#define I2C_NUMAKER_INIT(inst)                                                     \
	PINCTRL_DT_INST_DEFINE(inst);                                              \
                                                                                   \
	static void i2c_numaker_irq_config_func_##inst(const struct device *dev)   \
	{                                                                          \
		IRQ_CONNECT(DT_INST_IRQN(inst),                                    \
			    DT_INST_IRQ(inst, priority),                           \
			    i2c_numaker_isr,                                       \
			    DEVICE_DT_INST_GET(inst),                              \
			    0);                                                    \
                                                                                   \
		irq_enable(DT_INST_IRQN(inst));                                    \
	}                                                                          \
                                                                                   \
	static const struct i2c_numaker_config i2c_numaker_config_##inst = {       \
		.i2c_base = (I2C_T *) DT_INST_REG_ADDR(inst),                      \
		.reset = RESET_DT_SPEC_INST_GET(inst),                             \
		.clk_modidx = DT_INST_CLOCKS_CELL(inst, clock_module_index),       \
		.clk_src = DT_INST_CLOCKS_CELL(inst, clock_source),                \
		.clk_div = DT_INST_CLOCKS_CELL(inst, clock_divider),               \
		.clkctrl_dev = DEVICE_DT_GET(DT_PARENT(DT_INST_CLOCKS_CTLR(inst))),\
		.irq_n = DT_INST_IRQN(inst),                                       \
		.irq_config_func = i2c_numaker_irq_config_func_##inst,             \
		.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst),                    \
		.bitrate = DT_INST_PROP(inst, clock_frequency),                    \
	};                                                                         \
                                                                                   \
	static struct i2c_numaker_data i2c_numaker_data_##inst;                    \
                                                                                   \
	I2C_DEVICE_DT_INST_DEFINE(inst,                                            \
				  i2c_numaker_init,                                \
				  NULL,                                            \
				  &i2c_numaker_data_##inst,                        \
				  &i2c_numaker_config_##inst,                      \
				  POST_KERNEL,                                     \
				  CONFIG_I2C_INIT_PRIORITY,                        \
				  &i2c_numaker_driver_api);

DT_INST_FOREACH_STATUS_OKAY(I2C_NUMAKER_INIT);