Skip to content

Commit

Permalink
drivers: tacho: mchp: low power feature enabled
Browse files Browse the repository at this point in the history
Updated Tacho driver to support low power feature.

Signed-off-by: Manimaran A <manimaran.a@microchip.com>
  • Loading branch information
Manimaran-A authored and carlescufi committed Aug 16, 2023
1 parent b9fea02 commit a3d6b42
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions drivers/sensor/mchp_tach_xec/tach_mchp_xec.c
Expand Up @@ -21,6 +21,11 @@
#include <zephyr/sys/sys_io.h>
#include <zephyr/logging/log.h>

#include <zephyr/pm/device.h>
#ifdef CONFIG_PM_DEVICE
#include <zephyr/pm/policy.h>
#endif

LOG_MODULE_REGISTER(tach_xec, CONFIG_SENSOR_LOG_LEVEL);

struct tach_xec_config {
Expand All @@ -33,6 +38,7 @@ struct tach_xec_config {
};

struct tach_xec_data {
uint32_t control;
uint16_t count;
};

Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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, \
Expand Down

0 comments on commit a3d6b42

Please sign in to comment.