Skip to content
Permalink
Browse files

drivers: nrf: Add power management to nrf SPI driver.

Add power management to nrf SPI driver.

Signed-off-by: Mieszko Mierunski <mieszko.mierunski@nordicsemi.no>
  • Loading branch information...
Mierunski authored and carlescufi committed Jun 6, 2019
1 parent 23992a0 commit f4b999a389c98ea9279427634664ba8b5759193c
Showing with 72 additions and 20 deletions.
  1. +72 −20 drivers/spi/spi_nrfx_spi.c
@@ -18,10 +18,14 @@ struct spi_nrfx_data {
struct spi_context ctx;
size_t chunk_len;
bool busy;
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
u32_t pm_state;
#endif
};

struct spi_nrfx_config {
nrfx_spi_t spi;
nrfx_spi_t spi;
nrfx_spi_config_t config;
};

static inline struct spi_nrfx_data *get_dev_data(struct device *dev)
@@ -251,13 +255,13 @@ static void event_handler(const nrfx_spi_evt_t *p_event, void *p_context)
}
}

static int init_spi(struct device *dev, const nrfx_spi_config_t *config)
static int init_spi(struct device *dev)
{
/* This sets only default values of frequency, mode and bit order.
* The proper ones are set in configure() when a transfer is started.
*/
nrfx_err_t result = nrfx_spi_init(&get_dev_config(dev)->spi,
config,
&get_dev_config(dev)->config,
event_handler,
dev);
if (result != NRFX_SUCCESS) {
@@ -266,28 +270,65 @@ static int init_spi(struct device *dev, const nrfx_spi_config_t *config)
return -EBUSY;
}

#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
get_dev_data(dev)->pm_state = DEVICE_PM_ACTIVE_STATE;
#endif
spi_context_unlock_unconditionally(&get_dev_data(dev)->ctx);

return 0;
}

#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
static int spi_nrfx_pm_control(struct device *dev, u32_t ctrl_command,
void *context, device_pm_cb cb, void *arg)
{
int ret = 0;

if (ctrl_command == DEVICE_PM_SET_POWER_STATE) {
u32_t new_state = *((const u32_t *)context);

if (new_state != get_dev_data(dev)->pm_state) {
switch (new_state) {
case DEVICE_PM_ACTIVE_STATE:
init_spi(dev);
/* Force reconfiguration before next transfer */
get_dev_data(dev)->ctx.config = NULL;
break;

case DEVICE_PM_LOW_POWER_STATE:
case DEVICE_PM_SUSPEND_STATE:
case DEVICE_PM_OFF_STATE:
nrfx_spi_uninit(&get_dev_config(dev)->spi);
break;

default:
ret = -ENOTSUP;
}
if (!ret) {
get_dev_data(dev)->pm_state = new_state;
}
}
} else {
assert(ctrl_command == DEVICE_PM_GET_POWER_STATE);
*((u32_t *)context) = get_dev_data(dev)->pm_state;
}

if (cb) {
cb(dev, ret, context, arg);
}

return ret;
}
#endif /* CONFIG_DEVICE_POWER_MANAGEMENT */


#define SPI_NRFX_SPI_DEVICE(idx) \
static int spi_##idx##_init(struct device *dev) \
{ \
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_SPI##idx), \
DT_NORDIC_NRF_SPI_SPI_##idx##_IRQ_0_PRIORITY, \
nrfx_isr, nrfx_spi_##idx##_irq_handler, 0); \
const nrfx_spi_config_t config = { \
.sck_pin = DT_NORDIC_NRF_SPI_SPI_##idx##_SCK_PIN, \
.mosi_pin = DT_NORDIC_NRF_SPI_SPI_##idx##_MOSI_PIN, \
.miso_pin = DT_NORDIC_NRF_SPI_SPI_##idx##_MISO_PIN, \
.ss_pin = NRFX_SPI_PIN_NOT_USED, \
.orc = CONFIG_SPI_##idx##_NRF_ORC, \
.frequency = NRF_SPI_FREQ_4M, \
.mode = NRF_SPI_MODE_0, \
.bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST, \
}; \
return init_spi(dev, &config); \
return init_spi(dev); \
} \
static struct spi_nrfx_data spi_##idx##_data = { \
SPI_CONTEXT_INIT_LOCK(spi_##idx##_data, ctx), \
@@ -296,13 +337,24 @@ static int init_spi(struct device *dev, const nrfx_spi_config_t *config)
}; \
static const struct spi_nrfx_config spi_##idx##z_config = { \
.spi = NRFX_SPI_INSTANCE(idx), \
.config = { \
.sck_pin = DT_NORDIC_NRF_SPI_SPI_##idx##_SCK_PIN, \
.mosi_pin = DT_NORDIC_NRF_SPI_SPI_##idx##_MOSI_PIN, \
.miso_pin = DT_NORDIC_NRF_SPI_SPI_##idx##_MISO_PIN, \
.ss_pin = NRFX_SPI_PIN_NOT_USED, \
.orc = CONFIG_SPI_##idx##_NRF_ORC, \
.frequency = NRF_SPI_FREQ_4M, \
.mode = NRF_SPI_MODE_0, \
.bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST, \
} \
}; \
DEVICE_AND_API_INIT(spi_##idx, DT_NORDIC_NRF_SPI_SPI_##idx##_LABEL, \
spi_##idx##_init, \
&spi_##idx##_data, \
&spi_##idx##z_config, \
POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, \
&spi_nrfx_driver_api)
DEVICE_DEFINE(spi_##idx, DT_NORDIC_NRF_SPI_SPI_##idx##_LABEL, \
spi_##idx##_init, \
spi_nrfx_pm_control, \
&spi_##idx##_data, \
&spi_##idx##z_config, \
POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, \
&spi_nrfx_driver_api)

#ifdef CONFIG_SPI_0_NRF_SPI
SPI_NRFX_SPI_DEVICE(0);

0 comments on commit f4b999a

Please sign in to comment.
You can’t perform that action at this time.