Lines Matching +full:100 +full:base +full:- +full:t
5 * SPDX-License-Identifier: Apache-2.0
21 #include "i2c-priv.h"
30 I2C_Type *base; member
65 const struct mcux_flexcomm_config *config = dev->config; in mcux_flexcomm_configure()
66 struct mcux_flexcomm_data *data = dev->data; in mcux_flexcomm_configure()
67 I2C_Type *base = config->base; in mcux_flexcomm_configure() local
72 return -EINVAL; in mcux_flexcomm_configure()
76 return -EINVAL; in mcux_flexcomm_configure()
81 baudrate = KHZ(100); in mcux_flexcomm_configure()
90 return -EINVAL; in mcux_flexcomm_configure()
94 if (clock_control_get_rate(config->clock_dev, config->clock_subsys, in mcux_flexcomm_configure()
96 return -EINVAL; in mcux_flexcomm_configure()
99 k_sem_take(&data->lock, K_FOREVER); in mcux_flexcomm_configure()
100 I2C_MasterSetBaudRate(base, baudrate, clock_freq); in mcux_flexcomm_configure()
101 k_sem_give(&data->lock); in mcux_flexcomm_configure()
106 static void mcux_flexcomm_master_transfer_callback(I2C_Type *base, in mcux_flexcomm_master_transfer_callback() argument
114 ARG_UNUSED(base); in mcux_flexcomm_master_transfer_callback()
116 data->callback_status = status; in mcux_flexcomm_master_transfer_callback()
117 k_sem_give(&data->device_sync_sem); in mcux_flexcomm_master_transfer_callback()
139 const struct mcux_flexcomm_config *config = dev->config; in mcux_flexcomm_transfer()
140 struct mcux_flexcomm_data *data = dev->data; in mcux_flexcomm_transfer()
141 I2C_Type *base = config->base; in mcux_flexcomm_transfer() local
146 k_sem_take(&data->lock, K_FOREVER); in mcux_flexcomm_transfer()
150 if (I2C_MSG_ADDR_10_BITS & msgs->flags) { in mcux_flexcomm_transfer()
151 ret = -ENOTSUP; in mcux_flexcomm_transfer()
156 transfer.flags = mcux_flexcomm_convert_flags(msgs->flags); in mcux_flexcomm_transfer()
161 if (i != 0 && !(msgs->flags & I2C_MSG_RESTART)) { in mcux_flexcomm_transfer()
166 transfer.direction = (msgs->flags & I2C_MSG_READ) in mcux_flexcomm_transfer()
170 transfer.data = msgs->buf; in mcux_flexcomm_transfer()
171 transfer.dataSize = msgs->len; in mcux_flexcomm_transfer()
174 status = I2C_MasterTransferNonBlocking(base, in mcux_flexcomm_transfer()
175 &data->handle, &transfer); in mcux_flexcomm_transfer()
177 /* Return an error if the transfer didn't start successfully in mcux_flexcomm_transfer()
181 I2C_MasterTransferAbort(base, &data->handle); in mcux_flexcomm_transfer()
182 ret = -EIO; in mcux_flexcomm_transfer()
187 k_sem_take(&data->device_sync_sem, I2C_TRANSFER_TIMEOUT_MSEC); in mcux_flexcomm_transfer()
189 /* Return an error if the transfer didn't complete in mcux_flexcomm_transfer()
192 if (data->callback_status != kStatus_Success) { in mcux_flexcomm_transfer()
193 I2C_MasterTransferAbort(base, &data->handle); in mcux_flexcomm_transfer()
194 ret = -EIO; in mcux_flexcomm_transfer()
202 k_sem_give(&data->lock); in mcux_flexcomm_transfer()
215 for (i = 0; i < ARRAY_SIZE(data->target_data); i++) { in mcux_flexcomm_find_free_target()
216 target = &data->target_data[i]; in mcux_flexcomm_find_free_target()
217 if (!target->target_attached) { in mcux_flexcomm_find_free_target()
230 for (i = 0; i < ARRAY_SIZE(data->target_data); i++) { in mcux_flexcomm_find_target_by_address()
231 target = &data->target_data[i]; in mcux_flexcomm_find_target_by_address()
232 if (target->target_attached && target->target_cfg->address == address) { in mcux_flexcomm_find_target_by_address()
243 int idx = -1; in mcux_flexcomm_setup_i2c_config_address()
246 for (i = 0; i < ARRAY_SIZE(data->target_data); i++) { in mcux_flexcomm_setup_i2c_config_address()
247 if (data->target_data[i].target_attached && &data->target_data[i] == target) { in mcux_flexcomm_setup_i2c_config_address()
254 return -ENODEV; in mcux_flexcomm_setup_i2c_config_address()
261 addr = &data->i2c_cfg.address0; in mcux_flexcomm_setup_i2c_config_address()
264 addr = &data->i2c_cfg.address1; in mcux_flexcomm_setup_i2c_config_address()
267 addr = &data->i2c_cfg.address2; in mcux_flexcomm_setup_i2c_config_address()
270 addr = &data->i2c_cfg.address3; in mcux_flexcomm_setup_i2c_config_address()
273 return -1; in mcux_flexcomm_setup_i2c_config_address()
276 addr->address = target->target_cfg->address; in mcux_flexcomm_setup_i2c_config_address()
277 addr->addressDisable = disabled; in mcux_flexcomm_setup_i2c_config_address()
282 static void i2c_target_transfer_callback(I2C_Type *base, in i2c_target_transfer_callback() argument
285 /* Convert 8-bit received address to 7-bit address */ in i2c_target_transfer_callback()
286 uint8_t address = transfer->receivedAddress >> 1; in i2c_target_transfer_callback()
292 ARG_UNUSED(base); in i2c_target_transfer_callback()
300 target_cb = target->target_cfg->callbacks; in i2c_target_transfer_callback()
302 switch (transfer->event) { in i2c_target_transfer_callback()
305 if (target->first_read && target_cb->read_requested) { in i2c_target_transfer_callback()
306 target->first_read = false; in i2c_target_transfer_callback()
307 target_cb->read_requested(target->target_cfg, &txVal); in i2c_target_transfer_callback()
308 } else if (target_cb->read_processed) { in i2c_target_transfer_callback()
309 target_cb->read_processed(target->target_cfg, &txVal); in i2c_target_transfer_callback()
312 transfer->txData = &txVal; in i2c_target_transfer_callback()
313 transfer->txSize = 1; in i2c_target_transfer_callback()
318 if (target->first_write && target_cb->write_requested) { in i2c_target_transfer_callback()
319 target_cb->write_requested(target->target_cfg); in i2c_target_transfer_callback()
320 target->first_write = false; in i2c_target_transfer_callback()
323 transfer->rxData = &rxVal; in i2c_target_transfer_callback()
324 transfer->rxSize = 1; in i2c_target_transfer_callback()
325 target->is_write = true; in i2c_target_transfer_callback()
330 if (target->is_write && target_cb->write_received) { in i2c_target_transfer_callback()
331 target_cb->write_received(target->target_cfg, rxVal); in i2c_target_transfer_callback()
332 target->is_write = false; in i2c_target_transfer_callback()
337 if (target_cb->stop) { in i2c_target_transfer_callback()
338 target_cb->stop(target->target_cfg); in i2c_target_transfer_callback()
341 target->first_read = true; in i2c_target_transfer_callback()
342 target->first_write = true; in i2c_target_transfer_callback()
346 LOG_INF("Unhandled event: %d", transfer->event); in i2c_target_transfer_callback()
353 const struct mcux_flexcomm_config *config = dev->config; in mcux_flexcomm_setup_slave_config()
354 struct mcux_flexcomm_data *data = dev->data; in mcux_flexcomm_setup_slave_config()
355 I2C_Type *base = config->base; in mcux_flexcomm_setup_slave_config() local
359 if (clock_control_get_rate(config->clock_dev, config->clock_subsys, in mcux_flexcomm_setup_slave_config()
361 return -EINVAL; in mcux_flexcomm_setup_slave_config()
364 I2C_SlaveInit(base, &data->i2c_cfg, clock_freq); in mcux_flexcomm_setup_slave_config()
365 I2C_SlaveTransferCreateHandle(base, &data->target_handle, in mcux_flexcomm_setup_slave_config()
367 I2C_SlaveTransferNonBlocking(base, &data->target_handle, in mcux_flexcomm_setup_slave_config()
377 const struct mcux_flexcomm_config *config = dev->config; in mcux_flexcomm_target_register()
378 struct mcux_flexcomm_data *data = dev->data; in mcux_flexcomm_target_register()
380 I2C_Type *base = config->base; in mcux_flexcomm_target_register() local
382 I2C_MasterDeinit(base); in mcux_flexcomm_target_register()
385 return -EINVAL; in mcux_flexcomm_target_register()
390 return -EBUSY; in mcux_flexcomm_target_register()
393 target->target_cfg = target_config; in mcux_flexcomm_target_register()
394 target->target_attached = true; in mcux_flexcomm_target_register()
395 target->first_read = true; in mcux_flexcomm_target_register()
396 target->first_write = true; in mcux_flexcomm_target_register()
398 if (data->nr_targets_attached == 0) { in mcux_flexcomm_target_register()
399 I2C_SlaveGetDefaultConfig(&data->i2c_cfg); in mcux_flexcomm_target_register()
403 return -EINVAL; in mcux_flexcomm_target_register()
407 return -EINVAL; in mcux_flexcomm_target_register()
410 data->nr_targets_attached++; in mcux_flexcomm_target_register()
417 const struct mcux_flexcomm_config *config = dev->config; in mcux_flexcomm_target_unregister()
418 struct mcux_flexcomm_data *data = dev->data; in mcux_flexcomm_target_unregister()
420 I2C_Type *base = config->base; in mcux_flexcomm_target_unregister() local
422 target = mcux_flexcomm_find_target_by_address(data, target_config->address); in mcux_flexcomm_target_unregister()
423 if (!target || !target->target_attached) { in mcux_flexcomm_target_unregister()
424 return -EINVAL; in mcux_flexcomm_target_unregister()
428 return -EINVAL; in mcux_flexcomm_target_unregister()
431 target->target_cfg = NULL; in mcux_flexcomm_target_unregister()
432 target->target_attached = false; in mcux_flexcomm_target_unregister()
434 data->nr_targets_attached--; in mcux_flexcomm_target_unregister()
436 if (data->nr_targets_attached > 0) { in mcux_flexcomm_target_unregister()
439 return -EINVAL; in mcux_flexcomm_target_unregister()
443 I2C_SlaveDeinit(base); in mcux_flexcomm_target_unregister()
452 const struct mcux_flexcomm_config *config = dev->config; in mcux_flexcomm_isr()
453 struct mcux_flexcomm_data *data = dev->data; in mcux_flexcomm_isr()
454 I2C_Type *base = config->base; in mcux_flexcomm_isr() local
457 if (data->nr_targets_attached > 0) { in mcux_flexcomm_isr()
458 I2C_SlaveTransferHandleIRQ(base, &data->target_handle); in mcux_flexcomm_isr()
463 I2C_MasterTransferHandleIRQ(base, &data->handle); in mcux_flexcomm_isr()
468 const struct mcux_flexcomm_config *config = dev->config; in mcux_flexcomm_init()
469 struct mcux_flexcomm_data *data = dev->data; in mcux_flexcomm_init()
470 I2C_Type *base = config->base; in mcux_flexcomm_init() local
475 if (!device_is_ready(config->reset.dev)) { in mcux_flexcomm_init()
477 return -ENODEV; in mcux_flexcomm_init()
480 error = reset_line_toggle(config->reset.dev, config->reset.id); in mcux_flexcomm_init()
485 error = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT); in mcux_flexcomm_init()
490 k_sem_init(&data->lock, 1, 1); in mcux_flexcomm_init()
491 k_sem_init(&data->device_sync_sem, 0, K_SEM_MAX_LIMIT); in mcux_flexcomm_init()
493 if (!device_is_ready(config->clock_dev)) { in mcux_flexcomm_init()
495 return -ENODEV; in mcux_flexcomm_init()
499 if (clock_control_get_rate(config->clock_dev, config->clock_subsys, in mcux_flexcomm_init()
501 return -EINVAL; in mcux_flexcomm_init()
505 I2C_MasterInit(base, &master_config, clock_freq); in mcux_flexcomm_init()
506 I2C_MasterTransferCreateHandle(base, &data->handle, in mcux_flexcomm_init()
510 bitrate_cfg = i2c_map_dt_bitrate(config->bitrate); in mcux_flexcomm_init()
517 config->irq_config_func(dev); in mcux_flexcomm_init()
538 .base = (I2C_Type *) DT_INST_REG_ADDR(id), \