diff --git a/drivers/sensor/mchp_tach_xec/tach_mchp_xec.c b/drivers/sensor/mchp_tach_xec/tach_mchp_xec.c index ae9288964bbc07..49fbbac8fe2d50 100644 --- a/drivers/sensor/mchp_tach_xec/tach_mchp_xec.c +++ b/drivers/sensor/mchp_tach_xec/tach_mchp_xec.c @@ -21,6 +21,11 @@ #include #include +#include +#ifdef CONFIG_PM_DEVICE +#include +#endif + LOG_MODULE_REGISTER(tach_xec, CONFIG_SENSOR_LOG_LEVEL); struct tach_xec_config { @@ -33,6 +38,7 @@ struct tach_xec_config { }; struct tach_xec_data { + uint32_t control; uint16_t count; }; @@ -52,6 +58,9 @@ int tach_xec_sample_fetch(const struct device *dev, enum sensor_channel chan) struct tach_regs * const tach = cfg->regs; uint8_t poll_count = 0; +#ifdef CONFIG_PM_DEVICE + pm_policy_state_lock_get(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES); +#endif while (poll_count < PIN_STS_TIMEOUT) { /* See whether internal counter is already latched */ if (tach->STATUS & MCHP_TACH_STS_CNT_RDY) { @@ -65,7 +74,9 @@ int tach_xec_sample_fetch(const struct device *dev, enum sensor_channel chan) /* Allow other threads to run while we sleep */ k_usleep(USEC_PER_MSEC); } - +#ifdef CONFIG_PM_DEVICE + pm_policy_state_lock_put(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES); +#endif if (poll_count == PIN_STS_TIMEOUT) { return -EINVAL; } @@ -117,6 +128,36 @@ static void tach_xec_sleep_clr(const struct device *dev) #endif } +#ifdef CONFIG_PM_DEVICE +static int tach_xec_pm_action(const struct device *dev, enum pm_device_action action) +{ + const struct tach_xec_config * const cfg = dev->config; + struct tach_xec_data * const data = dev->data; + struct tach_regs * const tach = cfg->regs; + int ret = 0; + + switch (action) { + case PM_DEVICE_ACTION_RESUME: + if (data->control & MCHP_TACH_CTRL_EN) { + tach->CONTROL |= MCHP_TACH_CTRL_EN; + data->control &= (~MCHP_TACH_CTRL_EN); + } + break; + case PM_DEVICE_ACTION_SUSPEND: + if (tach->CONTROL & MCHP_TACH_CTRL_EN) { + /* Take a backup */ + data->control = tach->CONTROL; + tach->CONTROL &= (~MCHP_TACH_CTRL_EN); + } + break; + default: + ret = -ENOTSUP; + } + + return ret; +} +#endif /* CONFIG_PM_DEVICE */ + static int tach_xec_init(const struct device *dev) { const struct tach_xec_config * const cfg = dev->config; @@ -161,9 +202,11 @@ static const struct sensor_driver_api tach_xec_driver_api = { \ XEC_TACH_CONFIG(id); \ \ + PM_DEVICE_DT_INST_DEFINE(id, tach_xec_pm_action); \ + \ SENSOR_DEVICE_DT_INST_DEFINE(id, \ tach_xec_init, \ - NULL, \ + PM_DEVICE_DT_INST_GET(id), \ &tach_xec_data_##id, \ &tach_xec_config_##id, \ POST_KERNEL, \