Lines Matching +full:max +full:- +full:current +full:- +full:300
4 * SPDX-License-Identifier: Apache-2.0
25 #include "i2c-priv.h"
52 struct i2c_msg *current; member
59 I2C_CTL1(cfg->reg) |= I2C_CTL1_ERRIE; in i2c_gd32_enable_interrupts()
60 I2C_CTL1(cfg->reg) |= I2C_CTL1_EVIE; in i2c_gd32_enable_interrupts()
61 I2C_CTL1(cfg->reg) |= I2C_CTL1_BUFIE; in i2c_gd32_enable_interrupts()
66 I2C_CTL1(cfg->reg) &= ~I2C_CTL1_ERRIE; in i2c_gd32_disable_interrupts()
67 I2C_CTL1(cfg->reg) &= ~I2C_CTL1_EVIE; in i2c_gd32_disable_interrupts()
68 I2C_CTL1(cfg->reg) &= ~I2C_CTL1_BUFIE; in i2c_gd32_disable_interrupts()
74 data->current->len--; in i2c_gd32_xfer_read()
75 *data->current->buf = I2C_DATA(cfg->reg); in i2c_gd32_xfer_read()
76 data->current->buf++; in i2c_gd32_xfer_read()
78 if ((data->xfer_len > 0U) && in i2c_gd32_xfer_read()
79 (data->current->len == 0U)) { in i2c_gd32_xfer_read()
80 data->current++; in i2c_gd32_xfer_read()
87 data->current->len--; in i2c_gd32_xfer_write()
88 I2C_DATA(cfg->reg) = *data->current->buf; in i2c_gd32_xfer_write()
89 data->current->buf++; in i2c_gd32_xfer_write()
91 if ((data->xfer_len > 0U) && in i2c_gd32_xfer_write()
92 (data->current->len == 0U)) { in i2c_gd32_xfer_write()
93 data->current++; in i2c_gd32_xfer_write()
99 struct i2c_gd32_data *data = dev->data; in i2c_gd32_handle_rbne()
100 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_handle_rbne()
102 switch (data->xfer_len) { in i2c_gd32_handle_rbne()
105 k_sem_give(&data->sync_sem); in i2c_gd32_handle_rbne()
109 data->xfer_len--; in i2c_gd32_handle_rbne()
112 k_sem_give(&data->sync_sem); in i2c_gd32_handle_rbne()
124 I2C_CTL1(cfg->reg) &= ~I2C_CTL1_BUFIE; in i2c_gd32_handle_rbne()
131 data->xfer_len--; in i2c_gd32_handle_rbne()
140 struct i2c_gd32_data *data = dev->data; in i2c_gd32_handle_tbe()
141 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_handle_tbe()
143 if (data->xfer_len > 0U) { in i2c_gd32_handle_tbe()
144 data->xfer_len--; in i2c_gd32_handle_tbe()
145 if (data->xfer_len == 0U) { in i2c_gd32_handle_tbe()
150 I2C_CTL1(cfg->reg) &= ~I2C_CTL1_BUFIE; in i2c_gd32_handle_tbe()
156 I2C_CTL0(cfg->reg) |= I2C_CTL0_STOP; in i2c_gd32_handle_tbe()
158 k_sem_give(&data->sync_sem); in i2c_gd32_handle_tbe()
164 struct i2c_gd32_data *data = dev->data; in i2c_gd32_handle_btc()
165 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_handle_btc()
167 if (data->current->flags & I2C_MSG_READ) { in i2c_gd32_handle_btc()
170 switch (data->xfer_len) { in i2c_gd32_handle_btc()
173 I2C_CTL0(cfg->reg) |= I2C_CTL0_STOP; in i2c_gd32_handle_btc()
175 for (counter = 2U; counter > 0; counter--) { in i2c_gd32_handle_btc()
176 data->xfer_len--; in i2c_gd32_handle_btc()
180 k_sem_give(&data->sync_sem); in i2c_gd32_handle_btc()
185 I2C_CTL0(cfg->reg) &= ~I2C_CTL0_ACKEN; in i2c_gd32_handle_btc()
187 data->xfer_len--; in i2c_gd32_handle_btc()
202 struct i2c_gd32_data *data = dev->data; in i2c_gd32_handle_addsend()
203 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_handle_addsend()
205 if ((data->current->flags & I2C_MSG_READ) && (data->xfer_len <= 2U)) { in i2c_gd32_handle_addsend()
206 I2C_CTL0(cfg->reg) &= ~I2C_CTL0_ACKEN; in i2c_gd32_handle_addsend()
210 I2C_STAT0(cfg->reg); in i2c_gd32_handle_addsend()
211 I2C_STAT1(cfg->reg); in i2c_gd32_handle_addsend()
213 if (data->is_restart) { in i2c_gd32_handle_addsend()
214 data->is_restart = false; in i2c_gd32_handle_addsend()
215 data->current->flags &= ~I2C_MSG_RW_MASK; in i2c_gd32_handle_addsend()
216 data->current->flags |= I2C_MSG_READ; in i2c_gd32_handle_addsend()
218 I2C_CTL0(cfg->reg) |= I2C_CTL0_START; in i2c_gd32_handle_addsend()
223 if ((data->current->flags & I2C_MSG_READ) && (data->xfer_len == 1U)) { in i2c_gd32_handle_addsend()
225 I2C_CTL0(cfg->reg) |= I2C_CTL0_STOP; in i2c_gd32_handle_addsend()
231 struct i2c_gd32_data *data = dev->data; in i2c_gd32_event_isr()
232 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_event_isr()
235 stat = I2C_STAT0(cfg->reg); in i2c_gd32_event_isr()
238 if (data->current->flags & I2C_MSG_READ) { in i2c_gd32_event_isr()
239 I2C_DATA(cfg->reg) = (data->addr1 << 1U) | 1U; in i2c_gd32_event_isr()
241 I2C_DATA(cfg->reg) = (data->addr1 << 1U) | 0U; in i2c_gd32_event_isr()
244 I2C_DATA(cfg->reg) = data->addr2; in i2c_gd32_event_isr()
262 struct i2c_gd32_data *data = dev->data; in i2c_gd32_error_isr()
263 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_error_isr()
266 stat = I2C_STAT0(cfg->reg); in i2c_gd32_error_isr()
269 I2C_STAT0(cfg->reg) &= ~I2C_STAT0_BERR; in i2c_gd32_error_isr()
270 data->errs |= I2C_GD32_ERR_BERR; in i2c_gd32_error_isr()
274 I2C_STAT0(cfg->reg) &= ~I2C_STAT0_LOSTARB; in i2c_gd32_error_isr()
275 data->errs |= I2C_GD32_ERR_LARB; in i2c_gd32_error_isr()
279 I2C_STAT0(cfg->reg) &= ~I2C_STAT0_AERR; in i2c_gd32_error_isr()
280 data->errs |= I2C_GD32_ERR_AERR; in i2c_gd32_error_isr()
283 if (data->errs != 0U) { in i2c_gd32_error_isr()
285 I2C_CTL0(cfg->reg) |= I2C_CTL0_STOP; in i2c_gd32_error_isr()
287 k_sem_give(&data->sync_sem); in i2c_gd32_error_isr()
293 if (data->errs & I2C_GD32_ERR_BERR) { in i2c_gd32_log_err()
297 if (data->errs & I2C_GD32_ERR_LARB) { in i2c_gd32_log_err()
301 if (data->errs & I2C_GD32_ERR_AERR) { in i2c_gd32_log_err()
305 if (data->errs & I2C_GD32_ERR_BUSY) { in i2c_gd32_log_err()
312 struct i2c_gd32_data *data = dev->data; in i2c_gd32_xfer_begin()
313 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_xfer_begin()
315 k_sem_reset(&data->sync_sem); in i2c_gd32_xfer_begin()
317 data->errs = 0U; in i2c_gd32_xfer_begin()
318 data->is_restart = false; in i2c_gd32_xfer_begin()
321 I2C_CTL0(cfg->reg) |= I2C_CTL0_ACKEN; in i2c_gd32_xfer_begin()
323 if (data->current->flags & I2C_MSG_READ) { in i2c_gd32_xfer_begin()
325 if (data->xfer_len == 2U) { in i2c_gd32_xfer_begin()
326 I2C_CTL0(cfg->reg) |= I2C_CTL0_POAP; in i2c_gd32_xfer_begin()
335 if (data->dev_config & I2C_ADDR_10_BITS) { in i2c_gd32_xfer_begin()
336 data->is_restart = true; in i2c_gd32_xfer_begin()
337 data->current->flags &= ~I2C_MSG_RW_MASK; in i2c_gd32_xfer_begin()
344 I2C_CTL0(cfg->reg) |= I2C_CTL0_START; in i2c_gd32_xfer_begin()
349 struct i2c_gd32_data *data = dev->data; in i2c_gd32_xfer_end()
350 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_xfer_end()
355 while (I2C_STAT1(cfg->reg) & I2C_STAT1_I2CBSY) { in i2c_gd32_xfer_end()
359 if (data->errs) { in i2c_gd32_xfer_end()
360 return -EIO; in i2c_gd32_xfer_end()
368 struct i2c_gd32_data *data = dev->data; in i2c_gd32_msg_read()
369 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_msg_read()
371 if (I2C_STAT1(cfg->reg) & I2C_STAT1_I2CBSY) { in i2c_gd32_msg_read()
372 data->errs = I2C_GD32_ERR_BUSY; in i2c_gd32_msg_read()
373 return -EBUSY; in i2c_gd32_msg_read()
378 k_sem_take(&data->sync_sem, K_FOREVER); in i2c_gd32_msg_read()
385 struct i2c_gd32_data *data = dev->data; in i2c_gd32_msg_write()
386 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_msg_write()
388 if (I2C_STAT1(cfg->reg) & I2C_STAT1_I2CBSY) { in i2c_gd32_msg_write()
389 data->errs = I2C_GD32_ERR_BUSY; in i2c_gd32_msg_write()
390 return -EBUSY; in i2c_gd32_msg_write()
395 k_sem_take(&data->sync_sem, K_FOREVER); in i2c_gd32_msg_write()
405 struct i2c_gd32_data *data = dev->data; in i2c_gd32_transfer()
406 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_transfer()
407 struct i2c_msg *current, *next; in i2c_gd32_transfer() local
411 current = msgs; in i2c_gd32_transfer()
414 current->flags |= I2C_MSG_RESTART; in i2c_gd32_transfer()
419 next = current + 1; in i2c_gd32_transfer()
425 if ((current->flags & I2C_MSG_RW_MASK) != in i2c_gd32_transfer()
426 (next->flags & I2C_MSG_RW_MASK)) { in i2c_gd32_transfer()
427 if ((next->flags & I2C_MSG_RESTART) == 0U) { in i2c_gd32_transfer()
428 return -EINVAL; in i2c_gd32_transfer()
433 if (current->flags & I2C_MSG_STOP) { in i2c_gd32_transfer()
434 return -EINVAL; in i2c_gd32_transfer()
438 if ((current->buf == NULL) || in i2c_gd32_transfer()
439 (current->len == 0U)) { in i2c_gd32_transfer()
440 return -EINVAL; in i2c_gd32_transfer()
443 current++; in i2c_gd32_transfer()
446 k_sem_take(&data->bus_mutex, K_FOREVER); in i2c_gd32_transfer()
449 I2C_CTL0(cfg->reg) |= I2C_CTL0_I2CEN; in i2c_gd32_transfer()
451 if (data->dev_config & I2C_ADDR_10_BITS) { in i2c_gd32_transfer()
452 data->addr1 = 0xF0 | ((addr & BITS(8, 9)) >> 8U); in i2c_gd32_transfer()
453 data->addr2 = addr & BITS(0, 7); in i2c_gd32_transfer()
455 data->addr1 = addr & BITS(0, 6); in i2c_gd32_transfer()
459 data->current = &msgs[i]; in i2c_gd32_transfer()
460 data->xfer_len = msgs[i].len; in i2c_gd32_transfer()
463 if ((data->current->flags & I2C_MSG_RW_MASK) != in i2c_gd32_transfer()
467 data->xfer_len += msgs[itr].len; in i2c_gd32_transfer()
470 if (data->current->flags & I2C_MSG_READ) { in i2c_gd32_transfer()
483 I2C_CTL0(cfg->reg) &= ~I2C_CTL0_I2CEN; in i2c_gd32_transfer()
485 k_sem_give(&data->bus_mutex); in i2c_gd32_transfer()
493 struct i2c_gd32_data *data = dev->data; in i2c_gd32_configure()
494 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_configure()
498 k_sem_take(&data->bus_mutex, K_FOREVER); in i2c_gd32_configure()
501 I2C_CTL0(cfg->reg) &= ~I2C_CTL0_I2CEN; in i2c_gd32_configure()
504 (clock_control_subsys_t)&cfg->clkid, in i2c_gd32_configure()
510 LOG_ERR("I2C max clock freq %u, current is %u\n", in i2c_gd32_configure()
512 err = -ENOTSUP; in i2c_gd32_configure()
548 LOG_ERR("I2C standard-mode min clock freq %u, current is %u\n", in i2c_gd32_configure()
550 err = -ENOTSUP; in i2c_gd32_configure()
553 I2C_CTL1(cfg->reg) &= ~I2C_CTL1_I2CCLK; in i2c_gd32_configure()
554 I2C_CTL1(cfg->reg) |= freq; in i2c_gd32_configure()
556 /* Standard-mode risetime maximum value: 1000ns */ in i2c_gd32_configure()
558 I2C_RT(cfg->reg) = I2CCLK_MAX; in i2c_gd32_configure()
560 I2C_RT(cfg->reg) = freq + 1U; in i2c_gd32_configure()
566 I2C_CKCFG(cfg->reg) &= ~I2C_CKCFG_CLKC; in i2c_gd32_configure()
567 I2C_CKCFG(cfg->reg) |= clkc; in i2c_gd32_configure()
568 /* standard-mode */ in i2c_gd32_configure()
569 I2C_CKCFG(cfg->reg) &= ~I2C_CKCFG_FAST; in i2c_gd32_configure()
574 LOG_ERR("I2C fast-mode min clock freq %u, current is %u\n", in i2c_gd32_configure()
576 err = -ENOTSUP; in i2c_gd32_configure()
580 /* Fast-mode risetime maximum value: 300ns */ in i2c_gd32_configure()
581 I2C_RT(cfg->reg) = freq * 300U / 1000U + 1U; in i2c_gd32_configure()
590 I2C_CKCFG(cfg->reg) |= I2C_CKCFG_DTCY; in i2c_gd32_configure()
591 I2C_CKCFG(cfg->reg) &= ~I2C_CKCFG_CLKC; in i2c_gd32_configure()
592 I2C_CKCFG(cfg->reg) |= clkc; in i2c_gd32_configure()
593 /* Transfer mode: fast-mode */ in i2c_gd32_configure()
594 I2C_CKCFG(cfg->reg) |= I2C_CKCFG_FAST; in i2c_gd32_configure()
597 /* Disable transfer mode: fast-mode plus */ in i2c_gd32_configure()
598 I2C_FMPCFG(cfg->reg) &= ~I2C_FMPCFG_FMPEN; in i2c_gd32_configure()
605 LOG_ERR("I2C fast-mode plus min clock freq %u, current is %u\n", in i2c_gd32_configure()
607 err = -ENOTSUP; in i2c_gd32_configure()
611 /* Fast-mode plus risetime maximum value: 120ns */ in i2c_gd32_configure()
612 I2C_RT(cfg->reg) = freq * 120U / 1000U + 1U; in i2c_gd32_configure()
621 I2C_CKCFG(cfg->reg) |= I2C_CKCFG_DTCY; in i2c_gd32_configure()
622 I2C_CKCFG(cfg->reg) &= ~I2C_CKCFG_CLKC; in i2c_gd32_configure()
623 I2C_CKCFG(cfg->reg) |= clkc; in i2c_gd32_configure()
624 /* Transfer mode: fast-mode */ in i2c_gd32_configure()
625 I2C_CKCFG(cfg->reg) |= I2C_CKCFG_FAST; in i2c_gd32_configure()
627 /* Enable transfer mode: fast-mode plus */ in i2c_gd32_configure()
628 I2C_FMPCFG(cfg->reg) |= I2C_FMPCFG_FMPEN; in i2c_gd32_configure()
633 err = -EINVAL; in i2c_gd32_configure()
637 data->dev_config = dev_config; in i2c_gd32_configure()
639 k_sem_give(&data->bus_mutex); in i2c_gd32_configure()
654 struct i2c_gd32_data *data = dev->data; in i2c_gd32_init()
655 const struct i2c_gd32_config *cfg = dev->config; in i2c_gd32_init()
659 err = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT); in i2c_gd32_init()
664 /* Mutex semaphore to protect the i2c api in multi-thread env. */ in i2c_gd32_init()
665 k_sem_init(&data->bus_mutex, 1, 1); in i2c_gd32_init()
668 k_sem_init(&data->sync_sem, 0, K_SEM_MAX_LIMIT); in i2c_gd32_init()
671 (clock_control_subsys_t)&cfg->clkid); in i2c_gd32_init()
673 (void)reset_line_toggle_dt(&cfg->reset); in i2c_gd32_init()
675 cfg->irq_cfg_func(); in i2c_gd32_init()
677 bitrate_cfg = i2c_map_dt_bitrate(cfg->bitrate); in i2c_gd32_init()