diff --git a/drivers/display/display_st7735r.c b/drivers/display/display_st7735r.c index b5cbcab4811..1fe0948acfd 100644 --- a/drivers/display/display_st7735r.c +++ b/drivers/display/display_st7735r.c @@ -63,9 +63,6 @@ struct st7735r_data { const struct device *reset_dev; uint16_t x_offset; uint16_t y_offset; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; static void st7735r_set_lcd_margins(struct st7735r_data *data, @@ -462,10 +459,6 @@ static int st7735r_init(const struct device *dev) } } -#ifdef CONFIG_PM_DEVICE - data->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif - data->cmd_data_dev = device_get_binding(config->cmd_data.name); if (data->cmd_data_dev == NULL) { LOG_ERR("Could not get GPIO port for cmd/DATA port"); @@ -519,22 +512,14 @@ static int st7735r_pm_control(const struct device *dev, uint32_t ctrl_command, if (ret < 0) { return ret; } - data->pm_state = PM_DEVICE_STATE_ACTIVE; } else { ret = st7735r_enter_sleep(data); if (ret < 0) { return ret; } - data->pm_state = PM_DEVICE_STATE_LOW_POWER; } break; - - case PM_DEVICE_STATE_GET: - *state = data->pm_state; - - break; - default: ret = -EINVAL; } diff --git a/drivers/display/display_st7789v.c b/drivers/display/display_st7789v.c index 7473db6386d..807cc38aa81 100644 --- a/drivers/display/display_st7789v.c +++ b/drivers/display/display_st7789v.c @@ -52,9 +52,6 @@ struct st7789v_data { uint16_t width; uint16_t x_offset; uint16_t y_offset; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; #ifdef CONFIG_ST7789V_RGB565 @@ -375,10 +372,6 @@ static int st7789v_init(const struct device *dev) } #endif -#ifdef CONFIG_PM_DEVICE - data->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif - data->cmd_data_gpio = device_get_binding( DT_INST_GPIO_LABEL(0, cmd_data_gpios)); if (data->cmd_data_gpio == NULL) { @@ -418,17 +411,12 @@ static int st7789v_pm_control(const struct device *dev, uint32_t ctrl_command, case DEVICE_PM_SET_POWER_STATE: if (*state == PM_DEVICE_STATE_ACTIVE) { st7789v_exit_sleep(data); - data->pm_state = PM_DEVICE_STATE_ACTIVE; ret = 0; } else { st7789v_enter_sleep(data); - data->pm_state = PM_DEVICE_STATE_LOW_POWER; ret = 0; } break; - case PM_DEVICE_STATE_GET: - *state = data->pm_state; - break; default: ret = -EINVAL; } diff --git a/drivers/entropy/entropy_cc13xx_cc26xx.c b/drivers/entropy/entropy_cc13xx_cc26xx.c index 984dfb1d8a2..68a11f8d355 100644 --- a/drivers/entropy/entropy_cc13xx_cc26xx.c +++ b/drivers/entropy/entropy_cc13xx_cc26xx.c @@ -36,9 +36,6 @@ struct entropy_cc13xx_cc26xx_data { Power_NotifyObj post_notify; bool constrained; #endif -#ifdef CONFIG_PM_DEVICE - enum device_pm_state pm_state; -#endif }; static inline struct entropy_cc13xx_cc26xx_data * @@ -272,9 +269,11 @@ static int entropy_cc13xx_cc26xx_set_power_state(const struct device *dev, { struct entropy_cc13xx_cc26xx_data *data = get_dev_data(dev); int ret = 0; + enum pm_device_state state; - if ((new_state == PM_DEVICE_STATE_ACTIVE) && - (new_state != data->pm_state)) { + (void)pm_device_state_get(dev, &state); + + if ((new_state == PM_DEVICE_STATE_ACTIVE) && (new_state != state)) { Power_setDependency(PowerCC26XX_PERIPH_TRNG); start_trng(data); } else { @@ -282,12 +281,11 @@ static int entropy_cc13xx_cc26xx_set_power_state(const struct device *dev, new_state == PM_DEVICE_STATE_SUSPEND || new_state == PM_DEVICE_STATE_OFF); - if (data->pm_state == PM_DEVICE_STATE_ACTIVE) { + if (state == PM_DEVICE_STATE_ACTIVE) { stop_trng(data); Power_releaseDependency(PowerCC26XX_PERIPH_TRNG); } } - data->pm_state = new_state; return ret; } @@ -296,19 +294,15 @@ static int entropy_cc13xx_cc26xx_pm_control(const struct device *dev, uint32_t ctrl_command, enum pm_device_state *state) { - struct entropy_cc13xx_cc26xx_data *data = get_dev_data(dev); int ret = 0; if (ctrl_command == PM_DEVICE_STATE_SET) { - enum pm_device_state new_state = *state; + enum pm_device_state curr_state; - if (new_state != data->pm_state) { - ret = entropy_cc13xx_cc26xx_set_power_state(dev, - new_state); + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { + ret = entropy_cc13xx_cc26xx_set_power_state(dev, *state); } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = data->pm_state; } return ret; @@ -319,10 +313,6 @@ static int entropy_cc13xx_cc26xx_init(const struct device *dev) { struct entropy_cc13xx_cc26xx_data *data = get_dev_data(dev); -#ifdef CONFIG_PM_DEVICE - get_dev_data(dev)->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif - /* Initialize driver data */ ring_buf_init(&data->pool, sizeof(data->data), data->data); diff --git a/drivers/flash/spi_flash_at45.c b/drivers/flash/spi_flash_at45.c index 8b83688d8b0..959ba098944 100644 --- a/drivers/flash/spi_flash_at45.c +++ b/drivers/flash/spi_flash_at45.c @@ -60,9 +60,6 @@ struct spi_flash_at45_data { const struct device *spi; struct spi_cs_control spi_cs; struct k_sem lock; -#if IS_ENABLED(CONFIG_PM_DEVICE) - enum pm_device_state pm_state; -#endif }; struct spi_flash_at45_config { @@ -631,15 +628,15 @@ static int spi_flash_at45_pm_control(const struct device *dev, uint32_t ctrl_command, enum pm_device_state *state) { - struct spi_flash_at45_data *dev_data = get_dev_data(dev); const struct spi_flash_at45_config *dev_config = get_dev_config(dev); int err = 0; if (ctrl_command == PM_DEVICE_STATE_SET) { - enum pm_device_state new_state = *state; + enum pm_device_state curr_state; - if (new_state != dev_data->pm_state) { - switch (new_state) { + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { + switch (*state) { case PM_DEVICE_STATE_ACTIVE: acquire(dev); power_down_op(dev, CMD_EXIT_DPD, @@ -661,12 +658,7 @@ static int spi_flash_at45_pm_control(const struct device *dev, default: return -ENOTSUP; } - - dev_data->pm_state = new_state; } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = dev_data->pm_state; } return err; @@ -715,8 +707,6 @@ static const struct flash_driver_api spi_flash_at45_api = { }; \ static struct spi_flash_at45_data inst_##idx##_data = { \ .lock = Z_SEM_INITIALIZER(inst_##idx##_data.lock, 1, 1), \ - IF_ENABLED(CONFIG_PM_DEVICE, ( \ - .pm_state = PM_DEVICE_STATE_ACTIVE)) \ }; \ INST_RESET_GPIO_SPEC(idx) \ INST_WP_GPIO_SPEC(idx) \ diff --git a/drivers/gpio/gpio_dw.c b/drivers/gpio/gpio_dw.c index 292393f6583..e86bd3fb485 100644 --- a/drivers/gpio/gpio_dw.c +++ b/drivers/gpio/gpio_dw.c @@ -425,33 +425,15 @@ static inline int gpio_dw_manage_callback(const struct device *port, } #ifdef CONFIG_PM_DEVICE -static void gpio_dw_set_power_state(const struct device *port, - uint32_t power_state) -{ - struct gpio_dw_runtime *context = port->data; - - context->device_power_state = power_state; -} - -static uint32_t gpio_dw_get_power_state(const struct device *port) -{ - struct gpio_dw_runtime *context = port->data; - - return context->device_power_state; -} - static inline int gpio_dw_suspend_port(const struct device *port) { gpio_dw_clock_off(port); - gpio_dw_set_power_state(port, PM_DEVICE_STATE_SUSPEND); - return 0; } static inline int gpio_dw_resume_from_suspend_port(const struct device *port) { gpio_dw_clock_on(port); - gpio_dw_set_power_state(port, PM_DEVICE_STATE_ACTIVE); return 0; } @@ -471,15 +453,10 @@ static int gpio_dw_device_ctrl(const struct device *port, } else if (*state == PM_DEVICE_STATE_ACTIVE) { ret = gpio_dw_resume_from_suspend_port(port); } - } else if (ctrl_command == PM_DEVICE_STATE_GET) { - *state = gpio_dw_get_power_state(port); } return ret; } - -#else -#define gpio_dw_set_power_state(...) #endif #define gpio_dw_unmask_int(...) @@ -541,8 +518,6 @@ static int gpio_dw_initialize(const struct device *port) config->config_func(port); } - gpio_dw_set_power_state(port, PM_DEVICE_STATE_ACTIVE); - return 0; } diff --git a/drivers/gpio/gpio_dw.h b/drivers/gpio/gpio_dw.h index cc820aaa800..0ab0a19e712 100644 --- a/drivers/gpio/gpio_dw.h +++ b/drivers/gpio/gpio_dw.h @@ -41,9 +41,6 @@ struct gpio_dw_runtime { const struct device *clock; #endif sys_slist_t callbacks; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state device_power_state; -#endif }; #ifdef __cplusplus diff --git a/drivers/gpio/gpio_stm32.c b/drivers/gpio/gpio_stm32.c index e5f4427586c..89f0f1fdf42 100644 --- a/drivers/gpio/gpio_stm32.c +++ b/drivers/gpio/gpio_stm32.c @@ -451,9 +451,6 @@ static int gpio_stm32_port_toggle_bits(const struct device *dev, static int gpio_stm32_config(const struct device *dev, gpio_pin_t pin, gpio_flags_t flags) { -#ifdef CONFIG_PM_DEVICE_RUNTIME - struct gpio_stm32_data *data = dev->data; -#endif /* CONFIG_PM_DEVICE_RUNTIME */ int err = 0; int pincfg; @@ -466,8 +463,11 @@ static int gpio_stm32_config(const struct device *dev, } #ifdef CONFIG_PM_DEVICE_RUNTIME + enum pm_device_state state; + + (void)pm_device_state_get(dev, &state); /* Enable device clock before configuration (requires bank writes) */ - if (data->power_state != PM_DEVICE_STATE_ACTIVE) { + if (state != PM_DEVICE_STATE_ACTIVE) { err = pm_device_get(dev); if (err < 0) { return err; @@ -574,17 +574,9 @@ static const struct gpio_driver_api gpio_stm32_driver = { }; #ifdef CONFIG_PM_DEVICE -static uint32_t gpio_stm32_get_power_state(const struct device *dev) -{ - struct gpio_stm32_data *data = dev->data; - - return data->power_state; -} - static int gpio_stm32_set_power_state(const struct device *dev, enum pm_device_state new_state) { - struct gpio_stm32_data *data = dev->data; int ret = 0; if (new_state == PM_DEVICE_STATE_ACTIVE) { @@ -599,8 +591,6 @@ static int gpio_stm32_set_power_state(const struct device *dev, return ret; } - data->power_state = new_state; - return 0; } @@ -608,20 +598,17 @@ static int gpio_stm32_pm_device_ctrl(const struct device *dev, uint32_t ctrl_command, enum pm_device_state *state) { - struct gpio_stm32_data *data = dev->data; - uint32_t new_state; int ret = 0; switch (ctrl_command) { case PM_DEVICE_STATE_SET: - new_state = *state; - if (new_state != data->power_state) { - ret = gpio_stm32_set_power_state(dev, new_state); + enum pm_device_state curr_state; + + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { + ret = gpio_stm32_set_power_state(dev, *state); } break; - case PM_DEVICE_STATE_GET: - *state = gpio_stm32_get_power_state(dev); - break; default: ret = -EINVAL; @@ -657,14 +644,10 @@ static int gpio_stm32_init(const struct device *dev) #endif #ifdef CONFIG_PM_DEVICE_RUNTIME - data->power_state = PM_DEVICE_STATE_OFF; pm_device_enable(dev); return 0; #else -#ifdef CONFIG_PM_DEVICE - data->power_state = PM_DEVICE_STATE_ACTIVE; -#endif return gpio_stm32_clock_request(dev, true); #endif } diff --git a/drivers/gpio/gpio_stm32.h b/drivers/gpio/gpio_stm32.h index 98cb492d09f..324b1708a9b 100644 --- a/drivers/gpio/gpio_stm32.h +++ b/drivers/gpio/gpio_stm32.h @@ -229,10 +229,6 @@ struct gpio_stm32_data { const struct device *dev; /* user ISR cb */ sys_slist_t cb; -#ifdef CONFIG_PM_DEVICE - /* device power state */ - enum pm_device_state power_state; -#endif }; /** diff --git a/drivers/i2c/i2c_cc13xx_cc26xx.c b/drivers/i2c/i2c_cc13xx_cc26xx.c index c4a2f22c066..0f40a5b51ca 100644 --- a/drivers/i2c/i2c_cc13xx_cc26xx.c +++ b/drivers/i2c/i2c_cc13xx_cc26xx.c @@ -32,9 +32,6 @@ struct i2c_cc13xx_cc26xx_data { Power_NotifyObj postNotify; uint32_t dev_config; #endif -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; struct i2c_cc13xx_cc26xx_config { @@ -333,9 +330,11 @@ static int i2c_cc13xx_cc26xx_set_power_state(const struct device *dev, enum pm_device_state new_state) { int ret = 0; + enum pm_device_state state; - if ((new_state == PM_DEVICE_STATE_ACTIVE) && - (new_state != get_dev_data(dev)->pm_state)) { + (void)pm_device_state_get(dev, &state); + + if ((new_state == PM_DEVICE_STATE_ACTIVE) && (new_state != state) { Power_setDependency(PowerCC26XX_PERIPH_I2C0); IOCPinTypeI2c(get_dev_config(dev)->base, get_dev_config(dev)->sda_pin, @@ -344,14 +343,13 @@ static int i2c_cc13xx_cc26xx_set_power_state(const struct device *dev, get_dev_data(dev)->dev_config); if (ret == 0) { I2CMasterIntEnable(get_dev_config(dev)->base); - get_dev_data(dev)->pm_state = new_state; } } else { __ASSERT_NO_MSG(new_state == PM_DEVICE_STATE_LOW_POWER || new_state == PM_DEVICE_STATE_SUSPEND || new_state == PM_DEVICE_STATE_OFF); - if (get_dev_data(dev)->pm_state == PM_DEVICE_STATE_ACTIVE) { + if (state == PM_DEVICE_STATE_ACTIVE) { I2CMasterIntDisable(get_dev_config(dev)->base); I2CMasterDisable(get_dev_config(dev)->base); /* Reset pin type to default GPIO configuration */ @@ -360,7 +358,6 @@ static int i2c_cc13xx_cc26xx_set_power_state(const struct device *dev, IOCPortConfigureSet(get_dev_config(dev)->sda_pin, IOC_PORT_GPIO, IOC_STD_OUTPUT); Power_releaseDependency(PowerCC26XX_PERIPH_I2C0); - get_dev_data(dev)->pm_state = new_state; } } @@ -374,15 +371,13 @@ static int i2c_cc13xx_cc26xx_pm_control(const struct device *dev, int ret = 0; if (ctrl_command == PM_DEVICE_STATE_SET) { - enum pm_device_state new_state = *state; + enum pm_device_state curr_state; - if (new_state != get_dev_data(dev)->pm_state) { + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { ret = i2c_cc13xx_cc26xx_set_power_state(dev, new_state); } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = get_dev_data(dev)->pm_state; } return ret; @@ -394,10 +389,6 @@ static int i2c_cc13xx_cc26xx_init(const struct device *dev) uint32_t cfg; int err; -#ifdef CONFIG_PM_DEVICE - get_dev_data(dev)->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif - #ifdef CONFIG_PM /* Set Power dependencies & constraints */ Power_setDependency(PowerCC26XX_PERIPH_I2C0); diff --git a/drivers/i2c/i2c_nrfx_twi.c b/drivers/i2c/i2c_nrfx_twi.c index 560e922682b..7194cced5fd 100644 --- a/drivers/i2c/i2c_nrfx_twi.c +++ b/drivers/i2c/i2c_nrfx_twi.c @@ -19,9 +19,6 @@ struct i2c_nrfx_twi_data { struct k_sem completion_sync; volatile nrfx_err_t res; uint32_t dev_config; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; struct i2c_nrfx_twi_config { @@ -214,9 +211,6 @@ static int init_twi(const struct device *dev) dev->name); return -EBUSY; } -#ifdef CONFIG_PM_DEVICE - get_dev_data(dev)->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif return 0; } @@ -227,13 +221,13 @@ static int twi_nrfx_pm_control(const struct device *dev, enum pm_device_state *state) { int ret = 0; - enum pm_device_state pm_current_state = get_dev_data(dev)->pm_state; if (ctrl_command == PM_DEVICE_STATE_SET) { - enum pm_device_state new_state = *state; + enum pm_device_state curr_state; - if (new_state != pm_current_state) { - switch (new_state) { + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { + switch (*state) { case PM_DEVICE_STATE_ACTIVE: init_twi(dev); if (get_dev_data(dev)->dev_config) { @@ -246,7 +240,7 @@ static int twi_nrfx_pm_control(const struct device *dev, case PM_DEVICE_STATE_LOW_POWER: case PM_DEVICE_STATE_SUSPEND: case PM_DEVICE_STATE_OFF: - if (pm_current_state == PM_DEVICE_STATE_ACTIVE) { + if (curr_state == PM_DEVICE_STATE_ACTIVE) { nrfx_twi_uninit(&get_dev_config(dev)->twi); } break; @@ -254,13 +248,7 @@ static int twi_nrfx_pm_control(const struct device *dev, default: ret = -ENOTSUP; } - if (!ret) { - get_dev_data(dev)->pm_state = new_state; - } } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = get_dev_data(dev)->pm_state; } return ret; diff --git a/drivers/i2c/i2c_nrfx_twim.c b/drivers/i2c/i2c_nrfx_twim.c index c23021bd0bf..8eb06cbd5e9 100644 --- a/drivers/i2c/i2c_nrfx_twim.c +++ b/drivers/i2c/i2c_nrfx_twim.c @@ -22,9 +22,6 @@ struct i2c_nrfx_twim_data { uint32_t dev_config; uint16_t concat_buf_size; uint8_t *concat_buf; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; struct i2c_nrfx_twim_config { @@ -253,10 +250,6 @@ static int init_twim(const struct device *dev) return -EBUSY; } -#ifdef CONFIG_PM_DEVICE - get_dev_data(dev)->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif - return 0; } @@ -266,13 +259,13 @@ static int twim_nrfx_pm_control(const struct device *dev, enum pm_device_state *state) { int ret = 0; - enum pm_device_state pm_current_state = get_dev_data(dev)->pm_state; if (ctrl_command == PM_DEVICE_STATE_SET) { - enum pm_device_state new_state = *state; + enum pm_device_state curr_state; - if (new_state != pm_current_state) { - switch (new_state) { + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { + switch (*state) { case PM_DEVICE_STATE_ACTIVE: init_twim(dev); if (get_dev_data(dev)->dev_config) { @@ -285,7 +278,7 @@ static int twim_nrfx_pm_control(const struct device *dev, case PM_DEVICE_STATE_LOW_POWER: case PM_DEVICE_STATE_SUSPEND: case PM_DEVICE_STATE_OFF: - if (pm_current_state != PM_DEVICE_STATE_ACTIVE) { + if (curr_state != PM_DEVICE_STATE_ACTIVE) { break; } nrfx_twim_uninit(&get_dev_config(dev)->twim); @@ -294,13 +287,7 @@ static int twim_nrfx_pm_control(const struct device *dev, default: ret = -ENOTSUP; } - if (!ret) { - get_dev_data(dev)->pm_state = new_state; - } } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = get_dev_data(dev)->pm_state; } return ret; diff --git a/drivers/interrupt_controller/intc_arcv2_irq_unit.c b/drivers/interrupt_controller/intc_arcv2_irq_unit.c index 56cb1cc5528..001a62cdba0 100644 --- a/drivers/interrupt_controller/intc_arcv2_irq_unit.c +++ b/drivers/interrupt_controller/intc_arcv2_irq_unit.c @@ -32,8 +32,6 @@ extern void *_VectorTable; #define _ARC_V2_IRQ_VECT_BASE _ARC_V2_IRQ_VECT_BASE_S #endif -static enum pm_device_state _arc_v2_irq_unit_device_power_state = - PM_DEVICE_STATE_ACTIVE; struct arc_v2_irq_unit_ctx { uint32_t irq_ctrl; /* Interrupt Context Saving Control Register. */ uint32_t irq_vect_base; /* Interrupt Vector Base. */ @@ -121,8 +119,6 @@ static int arc_v2_irq_unit_suspend(const struct device *dev) ctx.irq_ctrl = z_arc_v2_aux_reg_read(_ARC_V2_AUX_IRQ_CTRL); ctx.irq_vect_base = z_arc_v2_aux_reg_read(_ARC_V2_IRQ_VECT_BASE); - _arc_v2_irq_unit_device_power_state = PM_DEVICE_STATE_SUSPEND; - return 0; } @@ -167,23 +163,9 @@ static int arc_v2_irq_unit_resume(const struct device *dev) #endif z_arc_v2_aux_reg_write(_ARC_V2_IRQ_VECT_BASE, ctx.irq_vect_base); - _arc_v2_irq_unit_device_power_state = PM_DEVICE_STATE_ACTIVE; - return 0; } -/* - * @brief Get the power state of interrupt unit - * - * @return the power state of interrupt unit - */ -static enum pm_device_state arc_v2_irq_unit_get_state(const struct device *dev) -{ - ARG_UNUSED(dev); - - return _arc_v2_irq_unit_device_power_state; -} - /* * @brief Implement the driver control of interrupt unit * @@ -205,8 +187,6 @@ static int arc_v2_irq_unit_device_ctrl(const struct device *dev, } else if (*state == PM_DEVICE_STATE_ACTIVE) { ret = arc_v2_irq_unit_resume(dev); } - } else if (ctrl_command == PM_DEVICE_STATE_GET) { - *state = arc_v2_irq_unit_get_state(dev); } arch_irq_unlock(key); diff --git a/drivers/interrupt_controller/intc_ioapic.c b/drivers/interrupt_controller/intc_ioapic.c index 30e9ced9f75..e9f6418be09 100644 --- a/drivers/interrupt_controller/intc_ioapic.c +++ b/drivers/interrupt_controller/intc_ioapic.c @@ -105,10 +105,6 @@ static __pinned_bss uint32_t ioapic_rtes; __pinned_bss uint32_t ioapic_suspend_buf[SUSPEND_BITS_REQD / 32] = {0}; - -__pinned_data -static uint32_t ioapic_device_power_state = PM_DEVICE_STATE_ACTIVE; - #endif static uint32_t __IoApicGet(int32_t offset); @@ -320,12 +316,14 @@ static int ioapic_device_ctrl(const struct device *dev, int ret = 0; if (ctrl_command == PM_DEVICE_STATE_SET) { + enum pm_device_state curr_state; + + (void)pm_device_state_get(dev, &curr_state); switch (*state) { case PM_DEVICE_STATE_LOW_POWER: break; case PM_DEVICE_STATE_ACTIVE: - if (ioapic_device_power_state != - PM_DEVICE_STATE_LOW_POWER) { + if (curr_state != PM_DEVICE_STATE_LOW_POWER) { ret = ioapic_resume_from_suspend(dev); } break; @@ -337,12 +335,6 @@ static int ioapic_device_ctrl(const struct device *dev, default: ret = -ENOTSUP; } - - if (ret == 0) { - ioapic_device_power_state = *state; - } - } else if (ctrl_command == PM_DEVICE_STATE_GET) { - *state = ioapic_device_power_state; } return ret; diff --git a/drivers/interrupt_controller/intc_loapic.c b/drivers/interrupt_controller/intc_loapic.c index 33da24c5d43..3599208588f 100644 --- a/drivers/interrupt_controller/intc_loapic.c +++ b/drivers/interrupt_controller/intc_loapic.c @@ -64,9 +64,6 @@ #include __pinned_bss uint32_t loapic_suspend_buf[LOPIC_SUSPEND_BITS_REQD / 32] = {0}; - -__pinned_data -static uint32_t loapic_device_power_state = PM_DEVICE_STATE_ACTIVE; #endif #ifdef DEVICE_MMIO_IS_IN_RAM @@ -372,7 +369,7 @@ static int loapic_suspend(const struct device *port) } } } - loapic_device_power_state = PM_DEVICE_STATE_SUSPEND; + return 0; } @@ -402,7 +399,6 @@ int loapic_resume(const struct device *port) } } } - loapic_device_power_state = PM_DEVICE_STATE_ACTIVE; return 0; } @@ -424,8 +420,6 @@ static int loapic_device_ctrl(const struct device *port, } else if (*state == PM_DEVICE_STATE_ACTIVE) { ret = loapic_resume(port); } - } else if (ctrl_command == PM_DEVICE_STATE_GET) { - *state = loapic_device_power_state; } return ret; diff --git a/drivers/led/led_pwm.c b/drivers/led/led_pwm.c index 366e54ce1b2..9c6d2719a5e 100644 --- a/drivers/led/led_pwm.c +++ b/drivers/led/led_pwm.c @@ -21,7 +21,6 @@ LOG_MODULE_REGISTER(led_pwm, CONFIG_LED_LOG_LEVEL); #define DEV_CFG(dev) ((const struct led_pwm_config *) ((dev)->config)) -#define DEV_DATA(dev) ((struct led_pwm_data *) ((dev)->data)) struct led_pwm { const struct device *dev; @@ -35,12 +34,6 @@ struct led_pwm_config { const struct led_pwm *led; }; -struct led_pwm_data { -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif -}; - static int led_pwm_blink(const struct device *dev, uint32_t led, uint32_t delay_on, uint32_t delay_off) { @@ -117,43 +110,20 @@ static int led_pwm_init(const struct device *dev) } } -#ifdef CONFIG_PM_DEVICE - struct led_pwm_data *data = DEV_DATA(dev); - - data->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif - return 0; } #ifdef CONFIG_PM_DEVICE -static int led_pwm_pm_get_state(const struct device *dev, - enum pm_device_state *state) -{ - struct led_pwm_data *data = DEV_DATA(dev); - - unsigned int key = irq_lock(); - *state = data->pm_state; - irq_unlock(key); - - return 0; -} - static int led_pwm_pm_set_state(const struct device *dev, enum pm_device_state new_state) { const struct led_pwm_config *config = DEV_CFG(dev); - struct led_pwm_data *data = DEV_DATA(dev); - enum pm_device_state old_state; - unsigned int key; + enum pm_device_state curr_state; - key = irq_lock(); - old_state = data->pm_state; - irq_unlock(key); + (void)pm_device_state_get(dev, &curr_state); - if (old_state == new_state) { - /* leave unchanged */ + if (curr_state == new_state) { return 0; } @@ -169,11 +139,6 @@ static int led_pwm_pm_set_state(const struct device *dev, } } - /* record the new state */ - key = irq_lock(); - data->pm_state = new_state; - irq_unlock(key); - return 0; } @@ -183,10 +148,6 @@ static int led_pwm_pm_control(const struct device *dev, uint32_t ctrl_command, int err; switch (ctrl_command) { - case PM_DEVICE_STATE_GET: - err = led_pwm_pm_get_state(dev, state); - break; - case PM_DEVICE_STATE_SET: err = led_pwm_pm_set_state(dev, *state); break; @@ -228,11 +189,8 @@ static const struct led_pwm_config led_pwm_config_##id = { \ .led = led_pwm_##id, \ }; \ \ -static struct led_pwm_data led_pwm_data_##id; \ - \ DEVICE_DT_INST_DEFINE(id, &led_pwm_init, led_pwm_pm_control, \ - &led_pwm_data_##id, &led_pwm_config_##id, \ - POST_KERNEL, CONFIG_LED_INIT_PRIORITY, \ - &led_pwm_api); + NULL, &led_pwm_config_##id, POST_KERNEL, \ + CONFIG_LED_INIT_PRIORITY, &led_pwm_api); DT_INST_FOREACH_STATUS_OKAY(LED_PWM_DEVICE) diff --git a/drivers/pwm/pwm_nrfx.c b/drivers/pwm/pwm_nrfx.c index 53768cc73dc..139511dbd09 100644 --- a/drivers/pwm/pwm_nrfx.c +++ b/drivers/pwm/pwm_nrfx.c @@ -319,40 +319,29 @@ static int pwm_nrfx_set_power_state(enum pm_device_state new_state, static int pwm_nrfx_pm_control(const struct device *dev, uint32_t ctrl_command, - enum pm_device_state *state, - enum pm_device_state *current_state) + enum pm_device_state *state) { int err = 0; if (ctrl_command == PM_DEVICE_STATE_SET) { - enum pm_device_state new_state = *state; + enum pm_device_state curr_state; - if (new_state != (*current_state)) { - err = pwm_nrfx_set_power_state(new_state, - *current_state, + (void)pm_device_state_get(dev, &curr_state); + if (*state != current_state) { + err = pwm_nrfx_set_power_state(*state, current_state, dev); - if (!err) { - *current_state = new_state; - } } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = *current_state; } return err; } -#define PWM_NRFX_PM_CONTROL(idx) \ - static int pwm_##idx##_nrfx_pm_control(const struct device *dev, \ - uint32_t ctrl_command, \ - enum pm_device_state *state) \ - { \ - static enum pm_device_state current_state = PM_DEVICE_STATE_ACTIVE; \ - int ret = 0; \ - ret = pwm_nrfx_pm_control(dev, ctrl_command, state, \ - ¤t_state); \ - return ret; \ +#define PWM_NRFX_PM_CONTROL(idx) \ + static int pwm_##idx##_nrfx_pm_control(const struct device *dev, \ + uint32_t ctrl_command, \ + enum pm_device_state *state) \ + { \ + return pwm_nrfx_pm_control(dev, ctrl_command, state) \ } #else diff --git a/drivers/sensor/apds9960/apds9960.c b/drivers/sensor/apds9960/apds9960.c index 95a8b1bfe1c..4f94cb6060e 100644 --- a/drivers/sensor/apds9960/apds9960.c +++ b/drivers/sensor/apds9960/apds9960.c @@ -439,8 +439,6 @@ static int apds9960_device_ctrl(const struct device *dev, } } - } else if (ctrl_command == PM_DEVICE_STATE_GET) { - *state = PM_DEVICE_STATE_ACTIVE; } return ret; diff --git a/drivers/sensor/bme280/bme280.c b/drivers/sensor/bme280/bme280.c index 0079997c250..2c1dcdc33a5 100644 --- a/drivers/sensor/bme280/bme280.c +++ b/drivers/sensor/bme280/bme280.c @@ -55,10 +55,6 @@ struct bme280_data { int32_t t_fine; uint8_t chip_id; - -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; /* Current power state */ -#endif }; struct bme280_config { @@ -199,8 +195,10 @@ static int bme280_sample_fetch(const struct device *dev, __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL); #ifdef CONFIG_PM_DEVICE + enum pm_device_state state; + (void)pm_device_state_get(dev, &state); /* Do not allow sample fetching from OFF state */ - if (data->pm_state == PM_DEVICE_STATE_OFF) + if (state == PM_DEVICE_STATE_OFF) return -EIO; #endif @@ -407,10 +405,6 @@ static int bme280_chip_init(const struct device *dev) /* Wait for the sensor to be ready */ k_sleep(K_MSEC(1)); -#ifdef CONFIG_PM_DEVICE - /* Set power state to ACTIVE */ - data->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif LOG_DBG("\"%s\" OK", dev->name); return 0; } @@ -419,16 +413,17 @@ static int bme280_chip_init(const struct device *dev) int bme280_pm_ctrl(const struct device *dev, uint32_t ctrl_command, enum pm_device_state *state) { - struct bme280_data *data = to_data(dev); - int ret = 0; /* Set power state */ if (ctrl_command == PM_DEVICE_STATE_SET) { - if (*state != data->pm_state) { + enum pm_device_state curr_state; + + pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { /* Switching from OFF to any */ - if (data->pm_state == PM_DEVICE_STATE_OFF) { + if (curr_state == PM_DEVICE_STATE_OFF) { /* Re-initialize the chip */ ret = bme280_chip_init(dev); @@ -445,17 +440,8 @@ int bme280_pm_ctrl(const struct device *dev, uint32_t ctrl_command, LOG_DBG("CTRL_MEAS write failed: %d", ret); } - - /* Store the new state */ - if (!ret) - data->pm_state = *state; } } - /* Get power state */ - else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = data->pm_state; - } return ret; } diff --git a/drivers/sensor/bmp388/bmp388.c b/drivers/sensor/bmp388/bmp388.c index fcaf2375547..bab5c5dcf3a 100644 --- a/drivers/sensor/bmp388/bmp388.c +++ b/drivers/sensor/bmp388/bmp388.c @@ -311,9 +311,10 @@ static int bmp388_attr_set(const struct device *dev, int ret; #ifdef CONFIG_PM_DEVICE - struct bmp388_data *data = DEV_DATA(dev); + enum pm_device_state state; - if (data->device_power_state != PM_DEVICE_STATE_ACTIVE) { + (void)pm_device_state_get(dev, &state); + if (state != PM_DEVICE_STATE_ACTIVE) { return -EBUSY; } #endif @@ -348,7 +349,10 @@ static int bmp388_sample_fetch(const struct device *dev, __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL); #ifdef CONFIG_PM_DEVICE - if (bmp388->device_power_state != PM_DEVICE_STATE_ACTIVE) { + enum pm_device_state state; + + (void)pm_device_state_get(dev, &state); + if (state != PM_DEVICE_STATE_ACTIVE) { return -EBUSY; } #endif @@ -549,10 +553,10 @@ static int bmp388_set_power_state(const struct device *dev, enum pm_device_state power_state) { uint8_t reg_val; + enum pm_device_state state; - struct bmp388_data *data = DEV_DATA(dev); - - if (data->device_power_state == power_state) { + (void)pm_device_state_get(dev, &state); + if (state == power_state) { /* We are already in the desired state. */ return 0; } @@ -574,18 +578,9 @@ static int bmp388_set_power_state(const struct device *dev, return -EIO; } - data->device_power_state = power_state; - return 0; } -static uint32_t bmp388_get_power_state(const struct device *dev) -{ - struct bmp388_data *ctx = DEV_DATA(dev); - - return ctx->device_power_state; -} - static int bmp388_device_ctrl( const struct device *dev, uint32_t ctrl_command, @@ -595,8 +590,6 @@ static int bmp388_device_ctrl( if (ctrl_command == PM_DEVICE_STATE_SET) { ret = bmp388_set_power_state(dev, *state); - } else if (ctrl_command == PM_DEVICE_STATE_GET) { - *state = bmp388_get_power_state(dev); } return ret; @@ -666,10 +659,6 @@ static int bmp388_init(const struct device *dev) return -ENODEV; } -#ifdef CONFIG_PM_DEVICE - bmp388->device_power_state = PM_DEVICE_STATE_ACTIVE; -#endif - /* Read calibration data */ if (bmp388_get_calibration_data(dev) < 0) { LOG_ERR("Failed to read calibration data."); diff --git a/drivers/sensor/bmp388/bmp388.h b/drivers/sensor/bmp388/bmp388.h index 7962ead4c76..52a2451ea7e 100644 --- a/drivers/sensor/bmp388/bmp388.h +++ b/drivers/sensor/bmp388/bmp388.h @@ -163,10 +163,6 @@ struct bmp388_data { uint8_t osr_temp; struct bmp388_cal_data cal; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state device_power_state; -#endif - #if defined(CONFIG_BMP388_TRIGGER) struct gpio_callback gpio_cb; #endif diff --git a/drivers/sensor/bmp388/bmp388_trigger.c b/drivers/sensor/bmp388/bmp388_trigger.c index f76928b0ec0..76ef6251c63 100644 --- a/drivers/sensor/bmp388/bmp388_trigger.c +++ b/drivers/sensor/bmp388/bmp388_trigger.c @@ -92,7 +92,10 @@ int bmp388_trigger_set( struct bmp388_data *data = DEV_DATA(dev); #ifdef CONFIG_PM_DEVICE - if (data->device_power_state != PM_DEVICE_STATE_ACTIVE) { + enum pm_device_state state; + + (void)pm_device_state_get(dev, &state); + if (state != PM_DEVICE_STATE_ACTIVE) { return -EBUSY; } #endif diff --git a/drivers/sensor/bq274xx/bq274xx.c b/drivers/sensor/bq274xx/bq274xx.c index 9e8a31ea774..d6bcaa3f748 100644 --- a/drivers/sensor/bq274xx/bq274xx.c +++ b/drivers/sensor/bq274xx/bq274xx.c @@ -691,8 +691,6 @@ static int bq274xx_enter_shutdown_mode(struct bq274xx_data *data) return status; } - data->pm_state = PM_DEVICE_STATE_OFF; - return 0; } @@ -756,9 +754,6 @@ static int bq274xx_pm_control(const struct device *dev, uint32_t ctrl_command, ret = -ENOTSUP; } break; - case PM_DEVICE_STATE_GET: - *state = data->pm_state; - break; default: ret = -EINVAL; } diff --git a/drivers/sensor/bq274xx/bq274xx.h b/drivers/sensor/bq274xx/bq274xx.h index d82f3511744..8831b734fc9 100644 --- a/drivers/sensor/bq274xx/bq274xx.h +++ b/drivers/sensor/bq274xx/bq274xx.h @@ -96,9 +96,6 @@ struct bq274xx_data { uint16_t remaining_charge_capacity; uint16_t nom_avail_capacity; uint16_t full_avail_capacity; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; struct bq274xx_config { diff --git a/drivers/sensor/fdc2x1x/fdc2x1x.c b/drivers/sensor/fdc2x1x/fdc2x1x.c index a7019066f02..2dea2d0c526 100644 --- a/drivers/sensor/fdc2x1x/fdc2x1x.c +++ b/drivers/sensor/fdc2x1x/fdc2x1x.c @@ -420,13 +420,6 @@ static int fdc2x1x_reset(const struct device *dev) FDC2X1X_RESET_DEV_MSK, FDC2X1X_RESET_DEV_SET(1)); - /* device defaults to sleep mode */ -#ifdef CONFIG_PM_DEVICE - struct fdc2x1x_data *data = dev->data; - - data->pm_state = PM_DEVICE_STATE_LOW_POWER; -#endif - return ret; } @@ -494,10 +487,13 @@ static int fdc2x1x_set_pm_state(const struct device *dev, int ret; struct fdc2x1x_data *data = dev->data; const struct fdc2x1x_config *cfg = dev->config; + enum pm_device_state curr_state; + + (void)pm_device_state_get(dev, &curr_state); switch (pm_state) { case PM_DEVICE_STATE_ACTIVE: - if (data->pm_state == PM_DEVICE_STATE_OFF) { + if (curr_state == PM_DEVICE_STATE_OFF) { ret = fdc2x1x_set_shutdown(dev, false); if (ret) { return ret; @@ -508,11 +504,10 @@ static int fdc2x1x_set_pm_state(const struct device *dev, if (ret) { return ret; } - data->pm_state = PM_DEVICE_STATE_ACTIVE; break; case PM_DEVICE_STATE_LOW_POWER: - if (data->pm_state == PM_DEVICE_STATE_OFF) { + if (curr_state == PM_DEVICE_STATE_OFF) { ret = fdc2x1x_set_shutdown(dev, false); if (ret) { return ret; @@ -522,13 +517,11 @@ static int fdc2x1x_set_pm_state(const struct device *dev, if (ret) { return ret; } - data->pm_state = PM_DEVICE_STATE_LOW_POWER; break; case PM_DEVICE_STATE_OFF: if (cfg->sd_gpio->name) { ret = fdc2x1x_set_shutdown(dev, true); - data->pm_state = PM_DEVICE_STATE_OFF; } else { LOG_ERR("SD pin not defined"); ret = -EINVAL; @@ -549,7 +542,10 @@ static int fdc2x1x_device_pm_ctrl(const struct device *dev, int ret = 0; if (ctrl_command == PM_DEVICE_STATE_SET) { - if (*state != data->pm_state) { + enum pm_device_state curr_state; + (void)pm_device_state_get(dev, &curr_state); + + if (*state != curr_state) { switch (*state) { case PM_DEVICE_STATE_ACTIVE: case PM_DEVICE_STATE_LOW_POWER: @@ -561,8 +557,6 @@ static int fdc2x1x_device_pm_ctrl(const struct device *dev, ret = -EINVAL; } } - } else if (ctrl_command == PM_DEVICE_STATE_GET) { - *state = data->pm_state; } return ret; @@ -648,8 +642,10 @@ static int fdc2x1x_sample_fetch(const struct device *dev, { #ifdef CONFIG_PM_DEVICE struct fdc2x1x_data *data = dev->data; + enum pm_device_state state; - if (data->pm_state != PM_DEVICE_STATE_ACTIVE) { + (void)pm_device_state_get(dev, &state); + if (state != PM_DEVICE_STATE_ACTIVE) { LOG_ERR("Sample fetch failed, device is not in active mode"); return -ENXIO; } @@ -984,11 +980,6 @@ static int fdc2x1x_init(const struct device *dev) if (fdc2x1x_set_op_mode(dev, FDC2X1X_ACTIVE_MODE) < 0) { return -EIO; } -#ifdef CONFIG_PM_DEVICE - struct fdc2x1x_data *data = dev->data; - - data->pm_state = FDC2X1X_ACTIVE_MODE; -#endif #ifdef CONFIG_FDC2X1X_TRIGGER if (fdc2x1x_init_interrupt(dev) < 0) { diff --git a/drivers/sensor/fdc2x1x/fdc2x1x.h b/drivers/sensor/fdc2x1x/fdc2x1x.h index b794b7c51af..39ea405f119 100644 --- a/drivers/sensor/fdc2x1x/fdc2x1x.h +++ b/drivers/sensor/fdc2x1x/fdc2x1x.h @@ -149,10 +149,6 @@ enum fdc2x1x_op_mode { struct fdc2x1x_data { bool fdc221x; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif - #ifdef CONFIG_FDC2X1X_TRIGGER struct gpio_callback gpio_cb; uint16_t int_config; diff --git a/drivers/sensor/fdc2x1x/fdc2x1x_trigger.c b/drivers/sensor/fdc2x1x/fdc2x1x_trigger.c index 51e27388887..496e5df05a9 100644 --- a/drivers/sensor/fdc2x1x/fdc2x1x_trigger.c +++ b/drivers/sensor/fdc2x1x/fdc2x1x_trigger.c @@ -24,8 +24,11 @@ static void fdc2x1x_thread_cb(const struct device *dev) uint16_t status; #ifdef CONFIG_PM_DEVICE + enum pm_device_state state; + /* INTB asserts after exiting shutdown mode. Drop this interrupt */ - if (drv_data->pm_state == PM_DEVICE_STATE_OFF) { + (void)pm_device_state_get(dev, &state); + if (state == PM_DEVICE_STATE_OFF) { return; } #endif diff --git a/drivers/sensor/lis2mdl/lis2mdl.c b/drivers/sensor/lis2mdl/lis2mdl.c index 9eacb3aadca..96439fdfa08 100644 --- a/drivers/sensor/lis2mdl/lis2mdl.c +++ b/drivers/sensor/lis2mdl/lis2mdl.c @@ -429,10 +429,6 @@ static int lis2mdl_init(const struct device *dev) } } -#ifdef CONFIG_PM_DEVICE - lis2mdl->power_state = PM_DEVICE_STATE_ACTIVE; -#endif - #ifdef CONFIG_LIS2MDL_TRIGGER if (cfg->trig_enabled) { if (lis2mdl_init_interrupt(dev) < 0) { @@ -464,7 +460,6 @@ static int lis2mdl_set_power_state(struct lis2mdl_data *lis2mdl, if (status) { LOG_ERR("Power up failed"); } - lis2mdl->power_state = PM_DEVICE_STATE_ACTIVE; LOG_DBG("State changed to active"); } else { __ASSERT_NO_MSG(new_state == PM_DEVICE_STATE_LOW_POWER || @@ -474,7 +469,6 @@ static int lis2mdl_set_power_state(struct lis2mdl_data *lis2mdl, if (status) { LOG_ERR("Power down failed"); } - lis2mdl->power_state = new_state; LOG_DBG("State changed to inactive"); } @@ -486,21 +480,18 @@ static int lis2mdl_pm_control(const struct device *dev, uint32_t ctrl_command, { struct lis2mdl_data *lis2mdl = dev->data; const struct lis2mdl_config *const config = dev->config; - enum pm_device_state current_state = lis2mdl->power_state; int status = 0; - enum pm_device_state new_state; switch (ctrl_command) { case PM_DEVICE_STATE_SET: - new_state = *state; - if (new_state != current_state) { + enum pm_device_state curr_state; + + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { status = lis2mdl_set_power_state(lis2mdl, config, - new_state); + *state); } break; - case PM_DEVICE_STATE_GET: - *state = current_state; - break; default: LOG_ERR("Got unknown power management control command"); status = -EINVAL; diff --git a/drivers/sensor/lis2mdl/lis2mdl.h b/drivers/sensor/lis2mdl/lis2mdl.h index 20f31025425..3f58f85d12c 100644 --- a/drivers/sensor/lis2mdl/lis2mdl.h +++ b/drivers/sensor/lis2mdl/lis2mdl.h @@ -50,11 +50,6 @@ struct lis2mdl_data { const struct device *dev; int16_t mag[3]; int16_t temp_sample; - -#ifdef CONFIG_PM_DEVICE - enum pm_device_state power_state; -#endif - struct k_sem fetch_sem; #ifdef CONFIG_LIS2MDL_TRIGGER diff --git a/drivers/sensor/qdec_nrfx/qdec_nrfx.c b/drivers/sensor/qdec_nrfx/qdec_nrfx.c index 0a34f90dcc1..cb4451b39c3 100644 --- a/drivers/sensor/qdec_nrfx/qdec_nrfx.c +++ b/drivers/sensor/qdec_nrfx/qdec_nrfx.c @@ -24,9 +24,6 @@ LOG_MODULE_REGISTER(qdec_nrfx, CONFIG_SENSOR_LOG_LEVEL); struct qdec_nrfx_data { int32_t acc; sensor_trigger_handler_t data_ready_handler; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; @@ -206,43 +203,24 @@ static int qdec_nrfx_init(const struct device *dev) qdec_nrfx_gpio_ctrl(true); nrfx_qdec_enable(); -#ifdef CONFIG_PM_DEVICE - struct qdec_nrfx_data *data = &qdec_nrfx_data; - - data->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif - return 0; } #ifdef CONFIG_PM_DEVICE -static int qdec_nrfx_pm_get_state(struct qdec_nrfx_data *data, - enum pm_device_state *state) -{ - unsigned int key = irq_lock(); - *state = data->pm_state; - irq_unlock(key); - - return 0; -} - static int qdec_nrfx_pm_set_state(struct qdec_nrfx_data *data, enum pm_device_state new_state) { - enum pm_device_state old_state; - unsigned int key; + enum pm_device_state curr_state; - key = irq_lock(); - old_state = data->pm_state; - irq_unlock(key); + (void)pm_device_state_get(dev, &curr_state); - if (old_state == new_state) { + if (curr_state == new_state) { /* leave unchanged */ return 0; } - if (old_state == PM_DEVICE_STATE_ACTIVE) { + if (curr_state == PM_DEVICE_STATE_ACTIVE) { /* device must be suspended */ nrfx_qdec_disable(); qdec_nrfx_gpio_ctrl(false); @@ -258,11 +236,6 @@ static int qdec_nrfx_pm_set_state(struct qdec_nrfx_data *data, nrfx_qdec_enable(); } - /* record the new state */ - key = irq_lock(); - data->pm_state = new_state; - irq_unlock(key); - return 0; } @@ -276,10 +249,6 @@ static int qdec_nrfx_pm_control(const struct device *dev, LOG_DBG(""); switch (ctrl_command) { - case PM_DEVICE_STATE_GET: - err = qdec_nrfx_pm_get_state(data, state); - break; - case PM_DEVICE_STATE_SET: err = qdec_nrfx_pm_set_state(data, *state); break; diff --git a/drivers/sensor/sgp40/sgp40.c b/drivers/sensor/sgp40/sgp40.c index 017ba7fffec..d2a206c4db1 100644 --- a/drivers/sensor/sgp40/sgp40.c +++ b/drivers/sensor/sgp40/sgp40.c @@ -189,11 +189,12 @@ static int sgp40_channel_get(const struct device *dev, static int sgp40_set_power_state(const struct device *dev, enum pm_device_state power_state) { - struct sgp40_data *data = dev->data; uint16_t cmd; int rc; + enum pm_device_state state; - if (data->pm_state == power_state) { + (void)pm_device_state_get(dev, &state); + if (state == power_state) { LOG_DBG("Device already in requested PM_STATE."); return 0; } @@ -214,18 +215,6 @@ static int sgp40_set_power_state(const struct device *dev, return rc; } - data->pm_state = power_state; - - return 0; -} - -static uint32_t sgp40_get_power_state(const struct device *dev, - enum pm_device_state *state) -{ - struct sgp40_data *data = dev->data; - - *state = data->pm_state; - return 0; } @@ -237,8 +226,6 @@ static int sgp40_pm_ctrl(const struct device *dev, if (ctrl_command == PM_DEVICE_STATE_SET) { rc = sgp40_set_power_state(dev, *state); - } else if (ctrl_command == PM_DEVICE_STATE_GET) { - rc = sgp40_get_power_state(dev, state); } return rc; diff --git a/drivers/sensor/sgp40/sgp40.h b/drivers/sensor/sgp40/sgp40.h index 856af8c33fe..f6d15a9a8af 100644 --- a/drivers/sensor/sgp40/sgp40.h +++ b/drivers/sensor/sgp40/sgp40.h @@ -47,10 +47,6 @@ struct sgp40_data { uint16_t raw_sample; int8_t rh_param[3]; int8_t t_param[3]; - -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; #endif /* ZEPHYR_DRIVERS_SENSOR_SGP40_SGP40_H_ */ diff --git a/drivers/sensor/vcnl4040/vcnl4040.c b/drivers/sensor/vcnl4040/vcnl4040.c index 338bbeabe33..9c452fbf464 100644 --- a/drivers/sensor/vcnl4040/vcnl4040.c +++ b/drivers/sensor/vcnl4040/vcnl4040.c @@ -273,8 +273,6 @@ static int vcnl4040_device_ctrl(const struct device *dev, #endif } - } else if (ctrl_command == PM_DEVICE_STATE_GET) { - *state = PM_DEVICE_STATE_ACTIVE; } return ret; diff --git a/drivers/serial/uart_cc13xx_cc26xx.c b/drivers/serial/uart_cc13xx_cc26xx.c index fb6de8e863e..b71eec3a44d 100644 --- a/drivers/serial/uart_cc13xx_cc26xx.c +++ b/drivers/serial/uart_cc13xx_cc26xx.c @@ -36,9 +36,6 @@ struct uart_cc13xx_cc26xx_data { bool tx_constrained; bool rx_constrained; #endif -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; static inline struct uart_cc13xx_cc26xx_data *get_dev_data(const struct device *dev) @@ -404,9 +401,11 @@ static int uart_cc13xx_cc26xx_set_power_state(const struct device *dev, enum pm_device_state new_state) { int ret = 0; + enum pm_device_state state; - if ((new_state == PM_DEVICE_STATE_ACTIVE) && - (new_state != get_dev_data(dev)->pm_state)) { + (void)pm_device_state_get(dev, &state); + + if ((new_state == PM_DEVICE_STATE_ACTIVE) && (new_state != state)) { if (get_dev_conf(dev)->regs == DT_INST_REG_ADDR(0)) { Power_setDependency(PowerCC26XX_PERIPH_UART0); @@ -416,15 +415,12 @@ static int uart_cc13xx_cc26xx_set_power_state(const struct device *dev, /* Configure and enable UART */ ret = uart_cc13xx_cc26xx_configure(dev, &get_dev_data(dev)->uart_config); - if (ret == 0) { - get_dev_data(dev)->pm_state = new_state; - } } else { __ASSERT_NO_MSG(new_state == PM_DEVICE_STATE_LOW_POWER || new_state == PM_DEVICE_STATE_SUSPEND || new_state == PM_DEVICE_STATE_OFF); - if (get_dev_data(dev)->pm_state == PM_DEVICE_STATE_ACTIVE) { + if (state == PM_DEVICE_STATE_ACTIVE) { UARTDisable(get_dev_conf(dev)->regs); /* * Release power dependency - i.e. potentially power @@ -438,7 +434,6 @@ static int uart_cc13xx_cc26xx_set_power_state(const struct device *dev, Power_releaseDependency( PowerCC26X2_PERIPH_UART1); } - get_dev_data(dev)->pm_state = new_state; } } @@ -452,15 +447,12 @@ static int uart_cc13xx_cc26xx_pm_control(const struct device *dev, int ret = 0; if (ctrl_command == PM_DEVICE_STATE_SET) { - enum pm_device_state new_state = *state; + enum pm_device_state curr_state; - if (new_state != get_dev_data(dev)->pm_state) { - ret = uart_cc13xx_cc26xx_set_power_state(dev, - new_state); + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { + ret = uart_cc13xx_cc26xx_set_power_state(dev, *state); } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = get_dev_data(dev)->pm_state; } return ret; @@ -578,13 +570,6 @@ static const struct uart_driver_api uart_cc13xx_cc26xx_driver_api = { #ifdef CONFIG_PM_DEVICE #define UART_CC13XX_CC26XX_DEVICE_INIT(n) \ UART_CC13XX_CC26XX_DEVICE_DEFINE(n) - -#define UART_CC13XX_CC26XX_INIT_PM_STATE \ - do { \ - get_dev_data(dev)->pm_state = PM_DEVICE_STATE_ACTIVE; \ - } while (0) -#else -#define UART_CC13XX_CC26XX_INIT_PM_STATE #endif #define UART_CC13XX_CC26XX_INIT_FUNC(n) \ @@ -592,8 +577,6 @@ static const struct uart_driver_api uart_cc13xx_cc26xx_driver_api = { { \ int ret; \ \ - UART_CC13XX_CC26XX_INIT_PM_STATE; \ - \ UART_CC13XX_CC26XX_POWER_UART(n); \ \ /* Configure IOC module to map UART signals to pins */ \ diff --git a/drivers/serial/uart_npcx.c b/drivers/serial/uart_npcx.c index 36971b3e650..1260aa2ac5f 100644 --- a/drivers/serial/uart_npcx.c +++ b/drivers/serial/uart_npcx.c @@ -40,9 +40,6 @@ struct uart_npcx_data { uart_irq_callback_user_data_t user_cb; void *user_data; #endif -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; /* Driver convenience defines */ @@ -441,20 +438,9 @@ static inline bool uart_npcx_device_is_transmitting(const struct device *dev) return 0; } -static inline int uart_npcx_get_power_state(const struct device *dev, - enum pm_device_state *state) -{ - const struct uart_npcx_data *const data = DRV_DATA(dev); - - *state = data->pm_state; - return 0; -} - static inline int uart_npcx_set_power_state(const struct device *dev, enum pm_device_state next_state) { - struct uart_npcx_data *const data = DRV_DATA(dev); - /* If next device power state is LOW or SUSPEND power state */ if (next_state == PM_DEVICE_STATE_LOW_POWER || next_state == PM_DEVICE_STATE_SUSPEND) { @@ -467,7 +453,6 @@ static inline int uart_npcx_set_power_state(const struct device *dev, } } - data->pm_state = next_state; return 0; } @@ -481,9 +466,6 @@ static int uart_npcx_pm_control(const struct device *dev, uint32_t ctrl_command, case PM_DEVICE_STATE_SET: ret = uart_npcx_set_power_state(dev, *state); break; - case PM_DEVICE_STATE_GET: - ret = uart_npcx_get_power_state(dev, state); - break; default: ret = -EINVAL; } diff --git a/drivers/serial/uart_nrfx_uart.c b/drivers/serial/uart_nrfx_uart.c index 66e4601598c..5317f0fafe5 100644 --- a/drivers/serial/uart_nrfx_uart.c +++ b/drivers/serial/uart_nrfx_uart.c @@ -1162,18 +1162,13 @@ static int uart_nrfx_pm_control(const struct device *dev, uint32_t ctrl_command, enum pm_device_state *state) { - static enum pm_device_state current_state = PM_DEVICE_STATE_ACTIVE; - if (ctrl_command == PM_DEVICE_STATE_SET) { - enum pm_device_state new_state = *state; + enum pm_device_state current_state; - if (new_state != current_state) { - uart_nrfx_set_power_state(dev, new_state); - current_state = new_state; + (void)pm_device_state_get(dev, ¤t_state); + if (*state != current_state) { + uart_nrfx_set_power_state(dev, *state); } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = current_state; } return 0; diff --git a/drivers/serial/uart_nrfx_uarte.c b/drivers/serial/uart_nrfx_uarte.c index e3fb9bcefbe..e044e419f57 100644 --- a/drivers/serial/uart_nrfx_uarte.c +++ b/drivers/serial/uart_nrfx_uarte.c @@ -124,9 +124,6 @@ struct uarte_nrfx_data { struct uarte_async_cb *async; #endif atomic_val_t poll_out_lock; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif uint8_t char_out; uint8_t rx_data; gppi_channel_t ppi_ch_endtx; @@ -543,7 +540,10 @@ static void tx_start(const struct device *dev, const uint8_t *buf, size_t len) NRF_UARTE_Type *uarte = get_uarte_instance(dev); #if CONFIG_PM_DEVICE - if (get_dev_data(dev)->pm_state != PM_DEVICE_STATE_ACTIVE) { + enum pm_device_state state; + + (void)pm_device_state_get(dev, &state); + if (state != PM_DEVICE_STATE_ACTIVE) { return; } #endif @@ -1700,10 +1700,6 @@ static int uarte_instance_init(const struct device *dev, return err; } -#ifdef CONFIG_PM_DEVICE - data->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif - if (IS_ENABLED(CONFIG_UART_ENHANCED_POLL_OUT) && get_dev_config(dev)->flags & UARTE_CFG_FLAG_PPI_ENDTX) { err = endtx_stoptx_ppi_init(uarte, data); @@ -1839,14 +1835,14 @@ static void uarte_nrfx_set_power_state(const struct device *dev, enum pm_device_state new_state) { NRF_UARTE_Type *uarte = get_uarte_instance(dev); +#if defined(CONFIG_UART_ASYNC_API) || defined(UARTE_INTERRUPT_DRIVEN) struct uarte_nrfx_data *data = get_dev_data(dev); +#endif if (new_state == PM_DEVICE_STATE_ACTIVE) { uarte_nrfx_pins_enable(dev, true); nrf_uarte_enable(uarte); - data->pm_state = new_state; - #ifdef CONFIG_UART_ASYNC_API if (hw_rx_counting_enabled(get_dev_data(dev))) { nrfx_timer_enable(&get_dev_config(dev)->timer); @@ -1868,6 +1864,8 @@ static void uarte_nrfx_set_power_state(const struct device *dev, #endif } } else { + enum pm_device_state state; + __ASSERT_NO_MSG(new_state == PM_DEVICE_STATE_LOW_POWER || new_state == PM_DEVICE_STATE_SUSPEND || new_state == PM_DEVICE_STATE_OFF); @@ -1875,12 +1873,11 @@ static void uarte_nrfx_set_power_state(const struct device *dev, /* if pm is already not active, driver will stay indefinitely * in while loop waiting for event NRF_UARTE_EVENT_RXTO */ - if (data->pm_state != PM_DEVICE_STATE_ACTIVE) { + (void)pm_device_state_get(dev, &state); + if (state != PM_DEVICE_STATE_ACTIVE) { return; } - data->pm_state = new_state; - /* Disabling UART requires stopping RX, but stop RX event is * only sent after each RX if async UART API is used. */ @@ -1928,17 +1925,15 @@ static int uarte_nrfx_pm_control(const struct device *dev, uint32_t ctrl_command, enum pm_device_state *state) { - struct uarte_nrfx_data *data = get_dev_data(dev); + enum pm_device_state curr_state; if (ctrl_command == PM_DEVICE_STATE_SET) { - enum pm_device_state new_state = *state; + enum pm_device_state curr_state; - if (new_state != data->pm_state) { - uarte_nrfx_set_power_state(dev, new_state); + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { + uarte_nrfx_set_power_state(dev, *state); } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = data->pm_state; } return 0; diff --git a/drivers/serial/uart_stm32.c b/drivers/serial/uart_stm32.c index 02016565079..b95a6c44102 100644 --- a/drivers/serial/uart_stm32.c +++ b/drivers/serial/uart_stm32.c @@ -1413,9 +1413,6 @@ static int uart_stm32_init(const struct device *dev) #if defined(CONFIG_UART_INTERRUPT_DRIVEN) || defined(CONFIG_UART_ASYNC_API) config->uconf.irq_config_func(dev); #endif -#ifdef CONFIG_PM_DEVICE - data->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif /* CONFIG_PM_DEVICE */ #ifdef CONFIG_UART_ASYNC_API return uart_stm32_async_init(dev); @@ -1429,7 +1426,6 @@ static int uart_stm32_set_power_state(const struct device *dev, enum pm_device_state new_state) { USART_TypeDef *UartInstance = UART_STRUCT(dev); - struct uart_stm32_data *data = DEV_DATA(dev); /* setting a low power mode */ if (new_state != PM_DEVICE_STATE_ACTIVE) { @@ -1449,7 +1445,7 @@ static int uart_stm32_set_power_state(const struct device *dev, LL_USART_ClearFlag_ORE(UartInstance); /* Leave UartInstance unchanged */ } - data->pm_state = new_state; + /* UartInstance returning to active mode has nothing special to do */ return 0; } @@ -1467,17 +1463,13 @@ static int uart_stm32_pm_control(const struct device *dev, uint32_t ctrl_command, enum pm_device_state *state) { - struct uart_stm32_data *data = DEV_DATA(dev); - if (ctrl_command == PM_DEVICE_STATE_SET) { - enum pm_device_state new_state = *state; + enum pm_device_state curr_state; - if (new_state != data->pm_state) { - uart_stm32_set_power_state(dev, new_state); + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { + uart_stm32_set_power_state(dev, *state); } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = data->pm_state; } return 0; diff --git a/drivers/serial/uart_stm32.h b/drivers/serial/uart_stm32.h index 05770020065..a0d09ac59c8 100644 --- a/drivers/serial/uart_stm32.h +++ b/drivers/serial/uart_stm32.h @@ -67,9 +67,6 @@ struct uart_stm32_data { uint8_t *rx_next_buffer; size_t rx_next_buffer_len; #endif -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; #endif /* ZEPHYR_DRIVERS_SERIAL_UART_STM32_H_ */ diff --git a/drivers/spi/spi_cc13xx_cc26xx.c b/drivers/spi/spi_cc13xx_cc26xx.c index 82ddd009f7b..1746eeb92b2 100644 --- a/drivers/spi/spi_cc13xx_cc26xx.c +++ b/drivers/spi/spi_cc13xx_cc26xx.c @@ -33,9 +33,6 @@ struct spi_cc13xx_cc26xx_config { struct spi_cc13xx_cc26xx_data { struct spi_context ctx; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; #define CPU_FREQ DT_PROP(DT_PATH(cpus, cpu_0), clock_frequency) @@ -215,22 +212,23 @@ static int spi_cc13xx_cc26xx_set_power_state(const struct device *dev, enum pm_device_state new_state) { int ret = 0; + enum pm_device_state state; - if ((new_state == PM_DEVICE_STATE_ACTIVE) && - (new_state != get_dev_data(dev)->pm_state)) { + (void)pm_device_state_get(dev, &state); + + if ((new_state == PM_DEVICE_STATE_ACTIVE) && (new_state != state)) { if (get_dev_config(dev)->base == DT_INST_REG_ADDR(0)) { Power_setDependency(PowerCC26XX_PERIPH_SSI0); } else { Power_setDependency(PowerCC26XX_PERIPH_SSI1); } - get_dev_data(dev)->pm_state = new_state; } else { __ASSERT_NO_MSG(new_state == PM_DEVICE_STATE_LOW_POWER || new_state == PM_DEVICE_STATE_SUSPEND || new_state == PM_DEVICE_STATE_OFF); - if (get_dev_data(dev)->pm_state == PM_DEVICE_STATE_ACTIVE) { + if (state == PM_DEVICE_STATE_ACTIVE) { SSIDisable(get_dev_config(dev)->base); /* * Release power dependency @@ -243,7 +241,6 @@ static int spi_cc13xx_cc26xx_set_power_state(const struct device *dev, Power_releaseDependency( PowerCC26XX_PERIPH_SSI1); } - get_dev_data(dev)->pm_state = new_state; } } @@ -257,15 +254,12 @@ static int spi_cc13xx_cc26xx_pm_control(const struct device *dev, int ret = 0; if (ctrl_command == PM_DEVICE_STATE_SET) { - uint32_t new_state = *state; + enum pm_device_state curr_state; - if (new_state != get_dev_data(dev)->pm_state) { - ret = spi_cc13xx_cc26xx_set_power_state(dev, - new_state); + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { + ret = spi_cc13xx_cc26xx_set_power_state(dev, *state); } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = get_dev_data(dev)->pm_state; } return ret; @@ -331,20 +325,9 @@ static const struct spi_driver_api spi_cc13xx_cc26xx_driver_api = { POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, \ &spi_cc13xx_cc26xx_driver_api) -#ifdef CONFIG_PM_DEVICE -#define SPI_CC13XX_CC26XX_INIT_PM_STATE \ - do { \ - get_dev_data(dev)->pm_state = PM_DEVICE_STATE_ACTIVE; \ - } while (0) -#else -#define SPI_CC13XX_CC26XX_INIT_PM_STATE -#endif - #define SPI_CC13XX_CC26XX_INIT_FUNC(n) \ static int spi_cc13xx_cc26xx_init_##n(const struct device *dev) \ { \ - SPI_CC13XX_CC26XX_INIT_PM_STATE; \ - \ SPI_CC13XX_CC26XX_POWER_SPI(n); \ \ spi_context_unlock_unconditionally(&get_dev_data(dev)->ctx);\ diff --git a/drivers/spi/spi_nrfx_spi.c b/drivers/spi/spi_nrfx_spi.c index aad83d99697..6214721a5e2 100644 --- a/drivers/spi/spi_nrfx_spi.c +++ b/drivers/spi/spi_nrfx_spi.c @@ -19,9 +19,6 @@ struct spi_nrfx_data { const struct device *dev; size_t chunk_len; bool busy; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif }; struct spi_nrfx_config { @@ -276,10 +273,6 @@ static int init_spi(const struct device *dev) return -EBUSY; } -#ifdef CONFIG_PM_DEVICE - dev_data->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif - return 0; } @@ -293,10 +286,11 @@ static int spi_nrfx_pm_control(const struct device *dev, const struct spi_nrfx_config *config = get_dev_config(dev); if (ctrl_command == PM_DEVICE_STATE_SET) { - enum pm_device_state new_state = *state; + enum pm_device_state curr_state; - if (new_state != data->pm_state) { - switch (new_state) { + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { + switch (*state) { case PM_DEVICE_STATE_ACTIVE: ret = init_spi(dev); /* Force reconfiguration before next transfer */ @@ -306,7 +300,7 @@ static int spi_nrfx_pm_control(const struct device *dev, case PM_DEVICE_STATE_LOW_POWER: case PM_DEVICE_STATE_SUSPEND: case PM_DEVICE_STATE_OFF: - if (data->pm_state == PM_DEVICE_STATE_ACTIVE) { + if (curr_state == PM_DEVICE_STATE_ACTIVE) { nrfx_spi_uninit(&config->spi); } break; @@ -314,13 +308,7 @@ static int spi_nrfx_pm_control(const struct device *dev, default: ret = -ENOTSUP; } - if (!ret) { - data->pm_state = new_state; - } } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = data->pm_state; } return ret; diff --git a/drivers/spi/spi_nrfx_spim.c b/drivers/spi/spi_nrfx_spim.c index 43502a6ec70..1389af160a1 100644 --- a/drivers/spi/spi_nrfx_spim.c +++ b/drivers/spi/spi_nrfx_spim.c @@ -20,9 +20,6 @@ struct spi_nrfx_data { const struct device *dev; size_t chunk_len; bool busy; -#ifdef CONFIG_PM_DEVICE - enum pm_device_state pm_state; -#endif #if (CONFIG_SPI_NRFX_RAM_BUFFER_SIZE > 0) uint8_t buffer[CONFIG_SPI_NRFX_RAM_BUFFER_SIZE]; #endif @@ -323,11 +320,6 @@ static int init_spim(const struct device *dev) return -EBUSY; } -#ifdef CONFIG_PM_DEVICE - data->pm_state = PM_DEVICE_STATE_ACTIVE; - get_dev_data(dev)->pm_state = PM_DEVICE_STATE_ACTIVE; -#endif - return 0; } @@ -341,10 +333,11 @@ static int spim_nrfx_pm_control(const struct device *dev, const struct spi_nrfx_config *config = get_dev_config(dev); if (ctrl_command == PM_DEVICE_STATE_SET) { - enum pm_device_state new_state = *state; + enum pm_device_state curr_state; - if (new_state != data->pm_state) { - switch (new_state) { + (void)pm_device_state_get(dev, &curr_state); + if (*state != curr_state) { + switch (*state) { case PM_DEVICE_STATE_ACTIVE: ret = init_spim(dev); /* Force reconfiguration before next transfer */ @@ -354,7 +347,7 @@ static int spim_nrfx_pm_control(const struct device *dev, case PM_DEVICE_STATE_LOW_POWER: case PM_DEVICE_STATE_SUSPEND: case PM_DEVICE_STATE_OFF: - if (data->pm_state == PM_DEVICE_STATE_ACTIVE) { + if (curr_state == PM_DEVICE_STATE_ACTIVE) { nrfx_spim_uninit(&config->spim); } break; @@ -362,13 +355,7 @@ static int spim_nrfx_pm_control(const struct device *dev, default: ret = -ENOTSUP; } - if (!ret) { - data->pm_state = new_state; - } } - } else { - __ASSERT_NO_MSG(ctrl_command == PM_DEVICE_STATE_GET); - *state = data->pm_state; } return ret; diff --git a/include/pm/device.h b/include/pm/device.h index d3b1c8a47e7..eec9d265e1b 100644 --- a/include/pm/device.h +++ b/include/pm/device.h @@ -121,20 +121,16 @@ int pm_device_state_set(const struct device *dev, enum pm_device_state device_power_state); /** - * @brief Call the get power state function of a device + * @brief Obtain the power state of a device. * - * This function lets the caller know the current device - * power state at any time. This state will be one of the defined - * power states allowed for the devices in that system - * - * @param dev pointer to device structure of the driver instance. - * @param device_power_state Device power state to be filled by the device + * @param dev Device instance. + * @param state Pointer where device power state will be stored. * * @retval 0 If successful. - * @retval Errno Negative errno code if failure. + * @retval -ENOSYS If device does not implement power management. */ int pm_device_state_get(const struct device *dev, - enum pm_device_state *device_power_state); + enum pm_device_state *state); #ifdef CONFIG_PM_DEVICE /** diff --git a/kernel/device.c b/kernel/device.c index d7a9e7ab0ee..a204d86122e 100644 --- a/kernel/device.c +++ b/kernel/device.c @@ -32,6 +32,7 @@ static inline void device_pm_state_init(const struct device *dev) .usage = ATOMIC_INIT(0), .lock = Z_MUTEX_INITIALIZER(dev->pm->lock), .condvar = Z_CONDVAR_INITIALIZER(dev->pm->condvar), + .state = PM_DEVICE_STATE_ACTIVE, }; #endif /* CONFIG_PM_DEVICE */ } diff --git a/samples/subsys/pm/device_pm/src/dummy_driver.c b/samples/subsys/pm/device_pm/src/dummy_driver.c index 5144c1681fc..d65dd03d4cf 100644 --- a/samples/subsys/pm/device_pm/src/dummy_driver.c +++ b/samples/subsys/pm/device_pm/src/dummy_driver.c @@ -9,12 +9,12 @@ #include "dummy_parent.h" #include "dummy_driver.h" -enum pm_device_state device_power_state; static const struct device *parent; static int dummy_open(const struct device *dev) { int ret; + enum pm_device_state state; printk("open()\n"); @@ -33,7 +33,8 @@ static int dummy_open(const struct device *dev) (void) pm_device_wait(dev, K_FOREVER); - if (dev->pm->state == PM_DEVICE_STATE_ACTIVE) { + (void)pm_device_state_get(dev, &state); + if (state == PM_DEVICE_STATE_ACTIVE) { printk("Dummy device resumed\n"); ret = 0; } else { @@ -85,27 +86,6 @@ static int dummy_close(const struct device *dev) return ret; } -static enum pm_device_state dummy_get_power_state(const struct device *dev) -{ - return device_power_state; -} - -static int dummy_suspend(const struct device *dev) -{ - printk("child suspending..\n"); - device_power_state = PM_DEVICE_STATE_SUSPEND; - - return 0; -} - -static int dummy_resume_from_suspend(const struct device *dev) -{ - printk("child resuming..\n"); - device_power_state = PM_DEVICE_STATE_ACTIVE; - - return 0; -} - static int dummy_device_pm_ctrl(const struct device *dev, uint32_t ctrl_command, enum pm_device_state *state) @@ -115,14 +95,13 @@ static int dummy_device_pm_ctrl(const struct device *dev, switch (ctrl_command) { case PM_DEVICE_STATE_SET: if (*state == PM_DEVICE_STATE_ACTIVE) { - ret = dummy_resume_from_suspend(dev); + printk("child resuming..\n"); + return 0; } else { - ret = dummy_suspend(dev); + printk("child suspending..\n"); + return 0; } break; - case PM_DEVICE_STATE_GET: - *state = dummy_get_power_state(dev); - break; default: ret = -EINVAL; @@ -146,7 +125,6 @@ int dummy_init(const struct device *dev) } pm_device_enable(dev); - device_power_state = PM_DEVICE_STATE_ACTIVE; return 0; } diff --git a/samples/subsys/pm/device_pm/src/dummy_parent.c b/samples/subsys/pm/device_pm/src/dummy_parent.c index 0a6641c3bb3..45888f9aa55 100644 --- a/samples/subsys/pm/device_pm/src/dummy_parent.c +++ b/samples/subsys/pm/device_pm/src/dummy_parent.c @@ -9,7 +9,6 @@ #include "dummy_parent.h" static uint32_t store_value; -enum pm_device_state parent_power_state; static int dummy_transfer(const struct device *dev, uint32_t cmd, uint32_t *val) @@ -25,27 +24,6 @@ static int dummy_transfer(const struct device *dev, uint32_t cmd, return 0; } -static enum pm_device_state dummy_get_power_state(const struct device *dev) -{ - return parent_power_state; -} - -static int dummy_suspend(const struct device *dev) -{ - printk("parent suspending..\n"); - parent_power_state = PM_DEVICE_STATE_SUSPEND; - - return 0; -} - -static int dummy_resume_from_suspend(const struct device *dev) -{ - printk("parent resuming..\n"); - parent_power_state = PM_DEVICE_STATE_ACTIVE; - - return 0; -} - static int dummy_parent_pm_ctrl(const struct device *dev, uint32_t ctrl_command, enum pm_device_state *state) @@ -55,14 +33,13 @@ static int dummy_parent_pm_ctrl(const struct device *dev, switch (ctrl_command) { case PM_DEVICE_STATE_SET: if (*state == PM_DEVICE_STATE_ACTIVE) { - ret = dummy_resume_from_suspend(dev); + printk("parent resuming..\n"); + return 0; } else { - ret = dummy_suspend(dev); + printk("parent suspending..\n"); + return 0; } break; - case PM_DEVICE_STATE_GET: - *state = dummy_get_power_state(dev); - break; default: ret = -EINVAL; @@ -78,7 +55,6 @@ static const struct dummy_parent_api funcs = { int dummy_parent_init(const struct device *dev) { pm_device_enable(dev); - parent_power_state = PM_DEVICE_STATE_ACTIVE; return 0; } diff --git a/subsys/pm/device.c b/subsys/pm/device.c index d69c09951fa..80b34b9cd77 100644 --- a/subsys/pm/device.c +++ b/subsys/pm/device.c @@ -131,23 +131,32 @@ const char *pm_device_state_str(enum pm_device_state state) int pm_device_state_set(const struct device *dev, enum pm_device_state device_power_state) { + int ret; + if (dev->pm_control == NULL) { return -ENOSYS; } - return dev->pm_control(dev, PM_DEVICE_STATE_SET, - &device_power_state); + ret = dev->pm_control(dev, PM_DEVICE_STATE_SET, &device_power_state); + if (ret < 0) { + return ret; + } + + dev->pm->state = device_power_state; + + return 0; } int pm_device_state_get(const struct device *dev, - enum pm_device_state *device_power_state) + enum pm_device_state *state) { if (dev->pm_control == NULL) { return -ENOSYS; } - return dev->pm_control(dev, PM_DEVICE_STATE_GET, - device_power_state); + *state = dev->pm->state; + + return 0; } bool pm_device_is_any_busy(void) diff --git a/tests/subsys/pm/device_runtime/src/dummy_driver.c b/tests/subsys/pm/device_runtime/src/dummy_driver.c index 32dc6cf3eb8..b5238d77d0b 100644 --- a/tests/subsys/pm/device_runtime/src/dummy_driver.c +++ b/tests/subsys/pm/device_runtime/src/dummy_driver.c @@ -9,8 +9,6 @@ #include #include "dummy_driver.h" -static uint32_t device_power_state; - static int dummy_wait(const struct device *dev) { return pm_device_wait(dev, K_FOREVER); @@ -36,39 +34,15 @@ static int dummy_close_sync(const struct device *dev) return pm_device_put(dev); } -static uint32_t dummy_get_power_state(const struct device *dev) -{ - return device_power_state; -} - -static int dummy_suspend(const struct device *dev) -{ - device_power_state = PM_DEVICE_STATE_SUSPEND; - return 0; -} - -static int dummy_resume_from_suspend(const struct device *dev) -{ - device_power_state = PM_DEVICE_STATE_ACTIVE; - return 0; -} - static int dummy_device_pm_ctrl(const struct device *dev, uint32_t ctrl_command, enum pm_device_state *state) { - int ret = 0; + int ret; switch (ctrl_command) { case PM_DEVICE_STATE_SET: - if (*state == PM_DEVICE_STATE_ACTIVE) { - ret = dummy_resume_from_suspend(dev); - } else { - ret = dummy_suspend(dev); - } - break; - case PM_DEVICE_STATE_GET: - *state = dummy_get_power_state(dev); + ret = 0; break; default: ret = -EINVAL; diff --git a/tests/subsys/pm/power_mgmt/src/dummy_driver.c b/tests/subsys/pm/power_mgmt/src/dummy_driver.c index 368e3a64840..cbf5237095a 100644 --- a/tests/subsys/pm/power_mgmt/src/dummy_driver.c +++ b/tests/subsys/pm/power_mgmt/src/dummy_driver.c @@ -9,8 +9,6 @@ #include #include "dummy_driver.h" -static enum pm_device_state device_power_state; - static int dummy_open(const struct device *dev) { return pm_device_get(dev); @@ -21,39 +19,15 @@ static int dummy_close(const struct device *dev) return pm_device_put(dev); } -static uint32_t dummy_get_power_state(const struct device *dev) -{ - return device_power_state; -} - -static int dummy_suspend(const struct device *dev) -{ - device_power_state = PM_DEVICE_STATE_SUSPEND; - return 0; -} - -static int dummy_resume_from_suspend(const struct device *dev) -{ - device_power_state = PM_DEVICE_STATE_ACTIVE; - return 0; -} - static int dummy_device_pm_ctrl(const struct device *dev, uint32_t ctrl_command, enum pm_device_state *state) { - int ret = 0; + int ret; switch (ctrl_command) { case PM_DEVICE_STATE_SET: - if (*state == PM_DEVICE_STATE_ACTIVE) { - ret = dummy_resume_from_suspend(dev); - } else { - ret = dummy_suspend(dev); - } - break; - case PM_DEVICE_STATE_GET: - *state = dummy_get_power_state(dev); + ret = 0; break; default: ret = -EINVAL;