drivers: i2c: cc13xx_cc26xx: add power management support

Add a dependency on the I2C resource in the TI Power module,
reconfigure the peripheral upon CPU exiting standby, and add
support for device PM.

Signed-off-by: Vincent Wan <vincent.wan@linaro.org>
This commit is contained in:
Vincent Wan 2019-11-06 14:56:13 -08:00 committed by Anas Nashif
commit f23940c678

View file

@ -6,6 +6,7 @@
#include <kernel.h>
#include <drivers/i2c.h>
#include <power/power.h>
#define LOG_LEVEL CONFIG_I2C_LOG_LEVEL
#include <logging/log.h>
@ -15,6 +16,9 @@ LOG_MODULE_REGISTER(i2c_cc13xx_cc26xx);
#include <driverlib/ioc.h>
#include <driverlib/prcm.h>
#include <ti/drivers/Power.h>
#include <ti/drivers/power/PowerCC26X2.h>
#include "i2c-priv.h"
DEVICE_DECLARE(i2c_cc13xx_cc26xx);
@ -23,6 +27,13 @@ struct i2c_cc13xx_cc26xx_data {
struct k_sem lock;
struct k_sem complete;
volatile u32_t error;
#ifdef CONFIG_SYS_POWER_MANAGEMENT
Power_NotifyObj postNotify;
u32_t dev_config;
#endif
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
u32_t pm_state;
#endif
};
struct i2c_cc13xx_cc26xx_config {
@ -193,6 +204,10 @@ static int i2c_cc13xx_cc26xx_transfer(struct device *dev, struct i2c_msg *msgs,
k_sem_take(&get_dev_data(dev)->lock, K_FOREVER);
#ifdef CONFIG_SYS_POWER_MANAGEMENT
sys_pm_ctrl_disable_state(SYS_POWER_STATE_SLEEP_2);
#endif
for (int i = 0; i < num_msgs; i++) {
/* Not supported by hardware */
if (msgs[i].flags & I2C_MSG_ADDR_10_BITS) {
@ -213,6 +228,10 @@ static int i2c_cc13xx_cc26xx_transfer(struct device *dev, struct i2c_msg *msgs,
}
}
#ifdef CONFIG_SYS_POWER_MANAGEMENT
sys_pm_ctrl_enable_state(SYS_POWER_STATE_SLEEP_2);
#endif
k_sem_give(&get_dev_data(dev)->lock);
return ret;
@ -250,6 +269,10 @@ static int i2c_cc13xx_cc26xx_configure(struct device *dev, u32_t dev_config)
I2CMasterInitExpClk(get_dev_config(dev)->base,
DT_CPU_CLOCK_FREQUENCY, fast);
#ifdef CONFIG_SYS_POWER_MANAGEMENT
get_dev_data(dev)->dev_config = dev_config;
#endif
return 0;
}
@ -267,11 +290,113 @@ static void i2c_cc13xx_cc26xx_isr(void *arg)
}
}
#ifdef CONFIG_SYS_POWER_MANAGEMENT
/*
* ======== postNotifyFxn ========
* Called by Power module when waking up the CPU from Standby. The i2c needs
* to be reconfigured afterwards, unless Zephyr's device PM turned it off, in
* which case it'd be responsible for turning it back on and reconfigure it.
*/
static int postNotifyFxn(unsigned int eventType, uintptr_t eventArg,
uintptr_t clientArg)
{
struct device *dev = (struct device *)clientArg;
int ret = Power_NOTIFYDONE;
s16_t res_id;
/* Reconfigure the hardware if returning from sleep */
if (eventType == PowerCC26XX_AWAKE_STANDBY) {
res_id = PowerCC26XX_PERIPH_I2C0;
if (Power_getDependencyCount(res_id) != 0) {
/* Reconfigure and enable I2C only if powered */
if (i2c_cc13xx_cc26xx_configure(dev,
get_dev_data(dev)->dev_config) != 0) {
ret = Power_NOTIFYERROR;
}
I2CMasterIntEnable(get_dev_config(dev)->base);
}
}
return (ret);
}
#endif
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
static int i2c_cc13xx_cc26xx_set_power_state(struct device *dev,
u32_t new_state)
{
int ret = 0;
if ((new_state == DEVICE_PM_ACTIVE_STATE) &&
(new_state != get_dev_data(dev)->pm_state)) {
Power_setDependency(PowerCC26XX_PERIPH_I2C0);
ret = i2c_cc13xx_cc26xx_configure(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 == DEVICE_PM_LOW_POWER_STATE ||
new_state == DEVICE_PM_SUSPEND_STATE ||
new_state == DEVICE_PM_OFF_STATE);
if (get_dev_data(dev)->pm_state == DEVICE_PM_ACTIVE_STATE) {
I2CMasterIntDisable(get_dev_config(dev)->base);
I2CMasterDisable(get_dev_config(dev)->base);
Power_releaseDependency(PowerCC26XX_PERIPH_I2C0);
get_dev_data(dev)->pm_state = new_state;
}
}
return ret;
}
static int i2c_cc13xx_cc26xx_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) {
ret = i2c_cc13xx_cc26xx_set_power_state(dev,
new_state);
}
} else {
__ASSERT_NO_MSG(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 */
static int i2c_cc13xx_cc26xx_init(struct device *dev)
{
u32_t cfg;
int err;
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
get_dev_data(dev)->pm_state = DEVICE_PM_ACTIVE_STATE;
#endif
#ifdef CONFIG_SYS_POWER_MANAGEMENT
/* Set Power dependencies & constraints */
Power_setDependency(PowerCC26XX_PERIPH_I2C0);
/* Register notification function */
Power_registerNotify(&get_dev_data(dev)->postNotify,
PowerCC26XX_AWAKE_STANDBY,
postNotifyFxn, (uintptr_t)dev);
#else
/* Enable I2C power domain */
PRCMPowerDomainOn(PRCM_DOMAIN_SERIAL);
@ -292,6 +417,7 @@ static int i2c_cc13xx_cc26xx_init(struct device *dev)
PRCM_DOMAIN_POWER_ON) {
continue;
}
#endif
IRQ_CONNECT(DT_INST_0_TI_CC13XX_CC26XX_I2C_IRQ_0,
DT_INST_0_TI_CC13XX_CC26XX_I2C_IRQ_0_PRIORITY,
@ -331,7 +457,16 @@ static struct i2c_cc13xx_cc26xx_data i2c_cc13xx_cc26xx_data = {
.error = I2C_MASTER_ERR_NONE
};
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
DEVICE_DEFINE(i2c_cc13xx_cc26xx, DT_INST_0_TI_CC13XX_CC26XX_I2C_LABEL,
i2c_cc13xx_cc26xx_init,
i2c_cc13xx_cc26xx_pm_control,
&i2c_cc13xx_cc26xx_data, &i2c_cc13xx_cc26xx_config,
POST_KERNEL, CONFIG_I2C_INIT_PRIORITY,
&i2c_cc13xx_cc26xx_driver_api);
#else
DEVICE_AND_API_INIT(i2c_cc13xx_cc26xx, DT_INST_0_TI_CC13XX_CC26XX_I2C_LABEL,
i2c_cc13xx_cc26xx_init, &i2c_cc13xx_cc26xx_data,
&i2c_cc13xx_cc26xx_config, POST_KERNEL,
CONFIG_I2C_INIT_PRIORITY, &i2c_cc13xx_cc26xx_driver_api);
#endif