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>
This commit is contained in:
parent
23992a0614
commit
f4b999a389
1 changed files with 72 additions and 20 deletions
|
@ -18,10 +18,14 @@ struct spi_nrfx_data {
|
||||||
struct spi_context ctx;
|
struct spi_context ctx;
|
||||||
size_t chunk_len;
|
size_t chunk_len;
|
||||||
bool busy;
|
bool busy;
|
||||||
|
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
|
||||||
|
u32_t pm_state;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
struct spi_nrfx_config {
|
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)
|
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.
|
/* This sets only default values of frequency, mode and bit order.
|
||||||
* The proper ones are set in configure() when a transfer is started.
|
* The proper ones are set in configure() when a transfer is started.
|
||||||
*/
|
*/
|
||||||
nrfx_err_t result = nrfx_spi_init(&get_dev_config(dev)->spi,
|
nrfx_err_t result = nrfx_spi_init(&get_dev_config(dev)->spi,
|
||||||
config,
|
&get_dev_config(dev)->config,
|
||||||
event_handler,
|
event_handler,
|
||||||
dev);
|
dev);
|
||||||
if (result != NRFX_SUCCESS) {
|
if (result != NRFX_SUCCESS) {
|
||||||
|
@ -266,28 +270,65 @@ static int init_spi(struct device *dev, const nrfx_spi_config_t *config)
|
||||||
return -EBUSY;
|
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);
|
spi_context_unlock_unconditionally(&get_dev_data(dev)->ctx);
|
||||||
|
|
||||||
return 0;
|
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) \
|
#define SPI_NRFX_SPI_DEVICE(idx) \
|
||||||
static int spi_##idx##_init(struct device *dev) \
|
static int spi_##idx##_init(struct device *dev) \
|
||||||
{ \
|
{ \
|
||||||
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_SPI##idx), \
|
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_SPI##idx), \
|
||||||
DT_NORDIC_NRF_SPI_SPI_##idx##_IRQ_0_PRIORITY, \
|
DT_NORDIC_NRF_SPI_SPI_##idx##_IRQ_0_PRIORITY, \
|
||||||
nrfx_isr, nrfx_spi_##idx##_irq_handler, 0); \
|
nrfx_isr, nrfx_spi_##idx##_irq_handler, 0); \
|
||||||
const nrfx_spi_config_t config = { \
|
return init_spi(dev); \
|
||||||
.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); \
|
|
||||||
} \
|
} \
|
||||||
static struct spi_nrfx_data spi_##idx##_data = { \
|
static struct spi_nrfx_data spi_##idx##_data = { \
|
||||||
SPI_CONTEXT_INIT_LOCK(spi_##idx##_data, ctx), \
|
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 = { \
|
static const struct spi_nrfx_config spi_##idx##z_config = { \
|
||||||
.spi = NRFX_SPI_INSTANCE(idx), \
|
.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, \
|
DEVICE_DEFINE(spi_##idx, DT_NORDIC_NRF_SPI_SPI_##idx##_LABEL, \
|
||||||
spi_##idx##_init, \
|
spi_##idx##_init, \
|
||||||
&spi_##idx##_data, \
|
spi_nrfx_pm_control, \
|
||||||
&spi_##idx##z_config, \
|
&spi_##idx##_data, \
|
||||||
POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, \
|
&spi_##idx##z_config, \
|
||||||
&spi_nrfx_driver_api)
|
POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, \
|
||||||
|
&spi_nrfx_driver_api)
|
||||||
|
|
||||||
#ifdef CONFIG_SPI_0_NRF_SPI
|
#ifdef CONFIG_SPI_0_NRF_SPI
|
||||||
SPI_NRFX_SPI_DEVICE(0);
|
SPI_NRFX_SPI_DEVICE(0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue