Skip to content

Commit

Permalink
drivers/smartbond: Fix PM device runtime support
Browse files Browse the repository at this point in the history
Removed PM device runtime support from drivers in PD_SYS domain.

Update the rest device drivers to call pm_device_runtime_get/put()
functions when CONFIG_PM_DEVICE_RUNTIME is enabled.

Signed-off-by: Ioannis Damigos <[email protected]>
  • Loading branch information
ydamigos authored and nashif committed Jun 18, 2024
1 parent 1fc26b8 commit 0a0bcca
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 208 deletions.
38 changes: 11 additions & 27 deletions drivers/adc/adc_smartbond_gpadc.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ struct adc_smartbond_data {
uint8_t sequence_channel_count;
/* Index in buffer to store current value to */
uint8_t result_index;
#if defined(CONFIG_PM_DEVICE)
/* Flag to prevent sleep */
ATOMIC_DEFINE(pm_policy_state_flag, 1);
#endif
};

#define SMARTBOND_ADC_CHANNEL_COUNT 8
Expand Down Expand Up @@ -115,37 +111,25 @@ static int adc_smartbond_channel_setup(const struct device *dev,
static inline void gpadc_smartbond_pm_policy_state_lock_get(const struct device *dev,
struct adc_smartbond_data *data)
{

#if defined(CONFIG_PM_DEVICE)
#if defined(CONFIG_PM_DEVICE_RUNTIME)
pm_device_runtime_get(dev);
#endif

if (!atomic_test_and_set_bit(data->pm_policy_state_flag, 0)) {
/*
* Prevent the SoC from entering the normal sleep state.
*/
pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
}

/*
* Prevent the SoC from entering the normal sleep state.
*/
pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
#endif
}

static inline void gpadc_smartbond_pm_policy_state_lock_put(const struct device *dev,
struct adc_smartbond_data *data)
{
#if defined(CONFIG_PM_DEVICE)
#if defined(CONFIG_PM_DEVICE_RUNTIME)
/*
* Allow the SoC to enter the normal sleep state once GPADC is done.
*/
pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
pm_device_runtime_put(dev);
#endif

if (atomic_test_and_clear_bit(data->pm_policy_state_flag, 0)) {
/*
* Allow the SoC to enter the normal sleep state once GPADC is done.
*/
pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
}
#endif
}

#define PER_CHANNEL_ADC_CONFIG_MASK (GPADC_GP_ADC_CTRL_REG_GP_ADC_SEL_Msk | \
Expand Down Expand Up @@ -259,8 +243,8 @@ static void adc_smartbond_isr(const struct device *dev)
data->channel_read_mask ^= 1 << current_channel;

if (data->channel_read_mask == 0) {
adc_context_on_sampling_done(&data->ctx, dev);
gpadc_smartbond_pm_policy_state_lock_put(dev, data);
adc_context_on_sampling_done(&data->ctx, dev);
} else {
adc_context_start_sampling(&data->ctx);
}
Expand All @@ -275,8 +259,8 @@ static int adc_smartbond_read(const struct device *dev,
int error;
struct adc_smartbond_data *data = dev->data;

gpadc_smartbond_pm_policy_state_lock_get(dev, data);
adc_context_lock(&data->ctx, false, NULL);
gpadc_smartbond_pm_policy_state_lock_get(dev, data);
error = start_read(dev, sequence);
adc_context_release(&data->ctx, error);

Expand All @@ -292,8 +276,8 @@ static int adc_smartbond_read_async(const struct device *dev,
struct adc_smartbond_data *data = dev->data;
int error;

gpadc_smartbond_pm_policy_state_lock_get(dev, data);
adc_context_lock(&data->ctx, true, async);
gpadc_smartbond_pm_policy_state_lock_get(dev, data);
error = start_read(dev, sequence);
adc_context_release(&data->ctx, error);

Expand Down
36 changes: 11 additions & 25 deletions drivers/adc/adc_smartbond_sdadc.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ struct sdadc_smartbond_data {
uint8_t sequence_channel_count;
/* Index in buffer to store current value to */
uint8_t result_index;
#if defined(CONFIG_PM_DEVICE)
/* Flag to prevent sleep */
ATOMIC_DEFINE(pm_policy_state_flag, 1);
#endif
};

#define SMARTBOND_SDADC_CHANNEL_COUNT 8
Expand Down Expand Up @@ -119,34 +115,24 @@ static inline void sdadc_smartbond_pm_policy_state_lock_get(const struct device
struct sdadc_smartbond_data *data)
{
#if defined(CONFIG_PM_DEVICE)
#if defined(CONFIG_PM_DEVICE_RUNTIME)
pm_device_runtime_get(dev);
#endif

if (!atomic_test_and_set_bit(data->pm_policy_state_flag, 0)) {
/*
* Prevent the SoC from entering the normal sleep state.
*/
pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
}
/*
* Prevent the SoC from entering the normal sleep state.
*/
pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
#endif
}

static inline void sdadc_smartbond_pm_policy_state_lock_put(const struct device *dev,
struct sdadc_smartbond_data *data)
{
#if defined(CONFIG_PM_DEVICE)
#if defined(CONFIG_PM_DEVICE_RUNTIME)
/*
* Allow the SoC to enter the normal sleep state once sdadc is done.
*/
pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
pm_device_runtime_put(dev);
#endif

if (atomic_test_and_clear_bit(data->pm_policy_state_flag, 0)) {
/*
* Allow the SoC to enter the normal sleep state once sdadc is done.
*/
pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
}
#endif
}


Expand Down Expand Up @@ -262,8 +248,8 @@ static void sdadc_smartbond_isr(const struct device *dev)
data->channel_read_mask ^= 1 << current_channel;

if (data->channel_read_mask == 0) {
adc_context_on_sampling_done(&data->ctx, dev);
sdadc_smartbond_pm_policy_state_lock_put(dev, data);
adc_context_on_sampling_done(&data->ctx, dev);
} else {
adc_context_start_sampling(&data->ctx);
}
Expand All @@ -278,8 +264,8 @@ static int sdadc_smartbond_read(const struct device *dev,
int error;
struct sdadc_smartbond_data *data = dev->data;

sdadc_smartbond_pm_policy_state_lock_get(dev, data);
adc_context_lock(&data->ctx, false, NULL);
sdadc_smartbond_pm_policy_state_lock_get(dev, data);
error = start_read(dev, sequence);
adc_context_release(&data->ctx, error);

Expand All @@ -295,8 +281,8 @@ static int sdadc_smartbond_read_async(const struct device *dev,
struct sdadc_smartbond_data *data = dev->data;
int error;

sdadc_smartbond_pm_policy_state_lock_get(dev, data);
adc_context_lock(&data->ctx, true, async);
sdadc_smartbond_pm_policy_state_lock_get(dev, data);
error = start_read(dev, sequence);
adc_context_release(&data->ctx, error);

Expand Down
55 changes: 23 additions & 32 deletions drivers/counter/counter_smartbond_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ struct counter_smartbond_data {
void *user_data;
uint32_t guard_period;
uint32_t freq;
#if defined(CONFIG_PM_DEVICE) || defined(CONFIG_PM_DEVICE_RUNTIME)
ATOMIC_DEFINE(pm_policy_state_flag, 1);
#if defined(CONFIG_PM_DEVICE)
uint8_t pdc_idx;
#endif
};
Expand All @@ -59,19 +58,17 @@ struct counter_smartbond_config {
LOG_INSTANCE_PTR_DECLARE(log);
};

#if defined(CONFIG_PM_DEVICE) || defined(CONFIG_PM_DEVICE_RUNTIME)
static void counter_smartbond_pm_policy_state_lock_get(struct counter_smartbond_data *data)
#if defined(CONFIG_PM_DEVICE)
static void counter_smartbond_pm_policy_state_lock_get(const struct device *dev)
{
if (!atomic_test_and_set_bit(data->pm_policy_state_flag, 0)) {
pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
}
pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
pm_device_runtime_get(dev);
}

static void counter_smartbond_pm_policy_state_lock_put(struct counter_smartbond_data *data)
static void counter_smartbond_pm_policy_state_lock_put(const struct device *dev)
{
if (atomic_test_and_clear_bit(data->pm_policy_state_flag, 0)) {
pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
}
pm_device_runtime_put(dev);
pm_policy_state_lock_put(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
}

/*
Expand Down Expand Up @@ -120,23 +117,18 @@ static void counter_smartbond_pdc_add(const struct device *dev)
struct counter_smartbond_data *data = dev->data;
uint8_t trigger = counter_smartbond_pdc_trigger_get(dev);

if (!atomic_test_and_set_bit(data->pm_policy_state_flag, 0)) {
data->pdc_idx = da1469x_pdc_add(trigger, MCU_PDC_MASTER_M33, PDC_XTAL_EN);
__ASSERT_NO_MSG(data->pdc_idx >= 0);

data->pdc_idx = da1469x_pdc_add(trigger, MCU_PDC_MASTER_M33, PDC_XTAL_EN);
__ASSERT_NO_MSG(data->pdc_idx >= 0);

da1469x_pdc_set(data->pdc_idx);
da1469x_pdc_ack(data->pdc_idx);
}
da1469x_pdc_set(data->pdc_idx);
da1469x_pdc_ack(data->pdc_idx);
}

static void counter_smartbond_pdc_del(const struct device *dev)
{
struct counter_smartbond_data *data = dev->data;

if (atomic_test_and_clear_bit(data->pm_policy_state_flag, 0)) {
da1469x_pdc_del(data->pdc_idx);
}
da1469x_pdc_del(data->pdc_idx);
}
#endif

Expand All @@ -145,24 +137,23 @@ static int counter_smartbond_start(const struct device *dev)
const struct counter_smartbond_config *config = dev->config;
TIMER2_Type *timer = config->timer;

/* Enable counter in free running mode */
timer->TIMER2_CTRL_REG |= (TIMER2_TIMER2_CTRL_REG_TIM_CLK_EN_Msk |
TIMER2_TIMER2_CTRL_REG_TIM_EN_Msk |
TIMER2_TIMER2_CTRL_REG_TIM_FREE_RUN_MODE_EN_Msk);

#if defined(CONFIG_PM_DEVICE) || defined(CONFIG_PM_DEVICE_RUNTIME)
#if defined(CONFIG_PM_DEVICE)
if (!counter_smartbond_is_sleep_allowed(dev)) {
struct counter_smartbond_data *data = dev->data;
/*
* Power mode constraints should be applied as long as the device
* is up and running.
*/
counter_smartbond_pm_policy_state_lock_get(data);
counter_smartbond_pm_policy_state_lock_get(dev);
} else {
counter_smartbond_pdc_add(dev);
}
#endif

/* Enable counter in free running mode */
timer->TIMER2_CTRL_REG |= (TIMER2_TIMER2_CTRL_REG_TIM_CLK_EN_Msk |
TIMER2_TIMER2_CTRL_REG_TIM_EN_Msk |
TIMER2_TIMER2_CTRL_REG_TIM_FREE_RUN_MODE_EN_Msk);

return 0;
}

Expand All @@ -178,9 +169,9 @@ static int counter_smartbond_stop(const struct device *dev)
TIMER2_TIMER2_CTRL_REG_TIM_CLK_EN_Msk);
data->callback = NULL;

#if defined(CONFIG_PM_DEVICE) || defined(CONFIG_PM_DEVICE_RUNTIME)
#if defined(CONFIG_PM_DEVICE)
if (!counter_smartbond_is_sleep_allowed(dev)) {
counter_smartbond_pm_policy_state_lock_put(data);
counter_smartbond_pm_policy_state_lock_put(dev);
} else {
counter_smartbond_pdc_del(dev);
}
Expand Down Expand Up @@ -421,7 +412,7 @@ static uint32_t counter_smartbond_get_freq(const struct device *dev)
return data->freq;
}

#if defined(CONFIG_PM_DEVICE) || defined(CONFIG_PM_DEVICE_RUNTIME)
#if defined(CONFIG_PM_DEVICE)
static void counter_smartbond_resume(const struct device *dev)
{
const struct counter_smartbond_config *cfg = dev->config;
Expand Down
10 changes: 1 addition & 9 deletions drivers/crypto/crypto_smartbond.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <da1469x_pd.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/pm/device.h>
#include <zephyr/pm/device_runtime.h>
#include <zephyr/pm/policy.h>
#include <zephyr/logging/log.h>

Expand Down Expand Up @@ -936,7 +935,7 @@ static const struct crypto_driver_api crypto_smartbond_driver_api = {
.query_hw_caps = crypto_smartbond_query_hw_caps
};

#if defined(CONFIG_PM_DEVICE) || defined(CONFIG_PM_DEVICE_RUNTIME)
#if defined(CONFIG_PM_DEVICE)
static int crypto_smartbond_pm_action(const struct device *dev,
enum pm_device_action action)
{
Expand Down Expand Up @@ -984,14 +983,7 @@ static int crypto_smartbond_init(const struct device *dev)
/* Controller should be initialized once a crypyographic session is requested */
crypto_smartbond_set_status(false);

#ifdef CONFIG_PM_DEVICE_RUNTIME
/* Make sure device state is marked as suspended */
pm_device_init_suspended(dev);

return pm_device_runtime_enable(dev);
#else
return 0;
#endif
}

/*
Expand Down
Loading

0 comments on commit 0a0bcca

Please sign in to comment.