Skip to content

Commit 3a4cc12

Browse files
Chenhongrenjhedberg
authored andcommitted
drivers: spi: it8xxx2: only enable spi clock during spi transaction
To reduce power consumption, the SPI clock is gated when no spi transaction is in progress. With this mechanism, current consumption is reduced by approximately 0.1mA when cpu idle. Signed-off-by: Ren Chen <[email protected]>
1 parent fdfece9 commit 3a4cc12

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

drivers/spi/spi_it8xxx2.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ LOG_MODULE_REGISTER(spi_it8xxx2, CONFIG_SPI_LOG_LEVEL);
6161
#define SPI25_CH1_CMD_ADDR_HB2 0x25
6262
#define SPI27_CH1_WR_MEM_ADDR_HB2 0x27
6363

64+
#define ECPM_BASE_ADDR DT_REG_ADDR(DT_NODELABEL(ecpm))
65+
#define ECPM05_CLK_GATING_CTRL3 0x05
66+
#define SSPI_CLOCK_GATING BIT(1)
67+
6468
struct spi_it8xxx2_cmdq_data {
6569
uint8_t spi_write_cmd_length;
6670

@@ -100,6 +104,23 @@ struct spi_it8xxx2_data {
100104
size_t receive_len;
101105
};
102106

107+
static inline void spi_it8xxx2_turn_on_clk(const struct device *dev, const bool enable)
108+
{
109+
ARG_UNUSED(dev);
110+
111+
uint8_t reg_val;
112+
unsigned int key = irq_lock();
113+
114+
reg_val = sys_read8(ECPM_BASE_ADDR + ECPM05_CLK_GATING_CTRL3);
115+
if (enable) {
116+
sys_write8(reg_val & ~SSPI_CLOCK_GATING, ECPM_BASE_ADDR + ECPM05_CLK_GATING_CTRL3);
117+
} else {
118+
sys_write8(reg_val | SSPI_CLOCK_GATING, ECPM_BASE_ADDR + ECPM05_CLK_GATING_CTRL3);
119+
}
120+
121+
irq_unlock(key);
122+
};
123+
103124
static inline int spi_it8xxx2_set_freq(const struct device *dev, const uint32_t frequency)
104125
{
105126
const struct spi_it8xxx2_config *cfg = dev->config;
@@ -209,11 +230,14 @@ static void spi_it8xxx2_complete(const struct device *dev, const int status)
209230
struct spi_it8xxx2_data *data = dev->data;
210231
struct spi_context *ctx = &data->ctx;
211232

212-
spi_context_complete(ctx, dev, status);
213-
214233
/* Permit to enter power policy and idle mode. */
215234
pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
216235
chip_permit_idle();
236+
237+
/* disable spi clock */
238+
spi_it8xxx2_turn_on_clk(dev, false);
239+
240+
spi_context_complete(ctx, dev, status);
217241
}
218242

219243
static inline void spi_it8xxx2_tx(const struct device *dev)
@@ -400,6 +424,9 @@ static int transceive(const struct device *dev, const struct spi_config *config,
400424
chip_block_idle();
401425
pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
402426

427+
/* enable spi clock */
428+
spi_it8xxx2_turn_on_clk(dev, true);
429+
403430
spi_context_buffers_setup(ctx, tx_bufs, rx_bufs, 1);
404431
ret = spi_it8xxx2_next_xfer(dev);
405432
if (!ret) {

0 commit comments

Comments
 (0)