Skip to content

Commit 5a19c65

Browse files
decsnykartben
authored andcommitted
drivers: i2s_mcux_sai: unduplicate code
move copy pasted code into common spot for when there is an invalid configuration passed to the config function Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
1 parent 6cbba80 commit 5a19c65

File tree

1 file changed

+21
-39
lines changed

1 file changed

+21
-39
lines changed

drivers/i2s/i2s_mcux_sai.c

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -438,63 +438,41 @@ static int i2s_mcux_config(const struct device *dev, enum i2s_dir dir,
438438
const struct i2s_mcux_config *dev_cfg = dev->config;
439439
I2S_Type *base = (I2S_Type *)dev_cfg->base;
440440
struct i2s_dev_data *dev_data = dev->data;
441+
enum i2s_state *tx_state = &(dev_data->tx.state);
442+
enum i2s_state *rx_state = &(dev_data->rx.state);
443+
uint8_t word_size_bits = i2s_cfg->word_size;
444+
uint8_t num_words = i2s_cfg->channels;
441445
sai_transceiver_t config;
446+
int ret = -EINVAL;
442447
uint32_t mclk;
443-
/*num_words is frame size*/
444-
uint8_t num_words = i2s_cfg->channels;
445-
uint8_t word_size_bits = i2s_cfg->word_size;
446448

447449
if ((dev_data->tx.state != I2S_STATE_NOT_READY) &&
448450
(dev_data->tx.state != I2S_STATE_READY) &&
449451
(dev_data->rx.state != I2S_STATE_NOT_READY) &&
450452
(dev_data->rx.state != I2S_STATE_READY)) {
451453
LOG_ERR("invalid state tx(%u) rx(%u)", dev_data->tx.state, dev_data->rx.state);
452-
if (dir == I2S_DIR_TX) {
453-
dev_data->tx.state = I2S_STATE_NOT_READY;
454-
} else {
455-
dev_data->rx.state = I2S_STATE_NOT_READY;
456-
}
457-
return -EINVAL;
454+
goto invalid_config;
458455
}
459456

460457
if (i2s_cfg->frame_clk_freq == 0U) {
461458
LOG_ERR("Invalid frame_clk_freq %u", i2s_cfg->frame_clk_freq);
462-
if (dir == I2S_DIR_TX) {
463-
dev_data->tx.state = I2S_STATE_NOT_READY;
464-
} else {
465-
dev_data->rx.state = I2S_STATE_NOT_READY;
466-
}
467-
return 0;
459+
goto invalid_config;
468460
}
469461

470462
if (word_size_bits < SAI_WORD_SIZE_BITS_MIN || word_size_bits > SAI_WORD_SIZE_BITS_MAX) {
471463
LOG_ERR("Unsupported I2S word size %u", word_size_bits);
472-
if (dir == I2S_DIR_TX) {
473-
dev_data->tx.state = I2S_STATE_NOT_READY;
474-
} else {
475-
dev_data->rx.state = I2S_STATE_NOT_READY;
476-
}
477-
return -EINVAL;
464+
goto invalid_config;
478465
}
479466

480467
if (num_words < SAI_WORD_PER_FRAME_MIN || num_words > SAI_WORD_PER_FRAME_MAX) {
481468
LOG_ERR("Unsupported words length %u", num_words);
482-
if (dir == I2S_DIR_TX) {
483-
dev_data->tx.state = I2S_STATE_NOT_READY;
484-
} else {
485-
dev_data->rx.state = I2S_STATE_NOT_READY;
486-
}
487-
return -EINVAL;
469+
goto invalid_config;
488470
}
489471

490472
if ((i2s_cfg->options & I2S_OPT_PINGPONG) == I2S_OPT_PINGPONG) {
491473
LOG_ERR("Ping-pong mode not supported");
492-
if (dir == I2S_DIR_TX) {
493-
dev_data->tx.state = I2S_STATE_NOT_READY;
494-
} else {
495-
dev_data->rx.state = I2S_STATE_NOT_READY;
496-
}
497-
return -ENOTSUP;
474+
ret = -ENOTSUP;
475+
goto invalid_config;
498476
}
499477

500478
memset(&config, 0, sizeof(config));
@@ -552,12 +530,8 @@ static int i2s_mcux_config(const struct device *dev, enum i2s_dir dir,
552530
break;
553531
default:
554532
LOG_ERR("Unsupported I2S data format");
555-
if (dir == I2S_DIR_TX) {
556-
dev_data->tx.state = I2S_STATE_NOT_READY;
557-
} else {
558-
dev_data->rx.state = I2S_STATE_NOT_READY;
559-
}
560-
return -EINVAL;
533+
ret = -EINVAL;
534+
goto invalid_config;
561535
}
562536

563537
/* sync mode configurations */
@@ -669,6 +643,14 @@ static int i2s_mcux_config(const struct device *dev, enum i2s_dir dir,
669643
}
670644

671645
return 0;
646+
647+
invalid_config:
648+
if (dir == I2S_DIR_TX) {
649+
*tx_state = I2S_STATE_NOT_READY;
650+
} else if (dir == I2S_DIR_RX) {
651+
*rx_state = I2S_STATE_NOT_READY;
652+
}
653+
return ret;
672654
}
673655

674656
const struct i2s_config *i2s_mcux_config_get(const struct device *dev, enum i2s_dir dir)

0 commit comments

Comments
 (0)