2022-01-06 20:17:39 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Commonwealth Scientific and Industrial Research
|
|
|
|
* Organisation (CSIRO) ABN 41 687 119 230.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT power_domain_gpio
|
|
|
|
|
2022-04-09 10:11:14 +08:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
#include <zephyr/pm/device.h>
|
|
|
|
#include <zephyr/pm/device_runtime.h>
|
2022-01-06 20:17:39 +08:00
|
|
|
|
2022-04-09 10:11:14 +08:00
|
|
|
#include <zephyr/logging/log.h>
|
2022-05-09 12:12:27 +08:00
|
|
|
LOG_MODULE_REGISTER(power_domain_gpio, CONFIG_POWER_DOMAIN_LOG_LEVEL);
|
2022-01-06 20:17:39 +08:00
|
|
|
|
|
|
|
struct pd_gpio_config {
|
|
|
|
struct gpio_dt_spec enable;
|
|
|
|
uint32_t startup_delay_us;
|
|
|
|
uint32_t off_on_delay_us;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pd_gpio_data {
|
2022-03-12 13:40:35 +08:00
|
|
|
k_timeout_t next_boot;
|
2022-01-06 20:17:39 +08:00
|
|
|
};
|
|
|
|
|
2023-01-22 10:32:15 +08:00
|
|
|
struct pd_visitor_context {
|
|
|
|
const struct device *domain;
|
|
|
|
enum pm_device_action action;
|
|
|
|
};
|
|
|
|
|
2023-07-15 16:10:28 +08:00
|
|
|
#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
|
|
|
|
|
2023-01-22 10:32:15 +08:00
|
|
|
static int pd_on_domain_visitor(const struct device *dev, void *context)
|
|
|
|
{
|
|
|
|
struct pd_visitor_context *visitor_context = context;
|
|
|
|
|
|
|
|
/* Only run action if the device is on the specified domain */
|
2024-01-10 18:01:11 +08:00
|
|
|
if (!dev->pm || (dev->pm_base->domain != visitor_context->domain)) {
|
2023-01-22 10:32:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)pm_device_action_run(dev, visitor_context->action);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-15 16:10:28 +08:00
|
|
|
#endif
|
|
|
|
|
2022-01-06 20:17:39 +08:00
|
|
|
static int pd_gpio_pm_action(const struct device *dev,
|
|
|
|
enum pm_device_action action)
|
|
|
|
{
|
2023-07-15 16:10:28 +08:00
|
|
|
#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
|
|
|
|
struct pd_visitor_context context = {.domain = dev};
|
|
|
|
#endif
|
2022-01-06 20:17:39 +08:00
|
|
|
const struct pd_gpio_config *cfg = dev->config;
|
2022-03-12 13:40:35 +08:00
|
|
|
struct pd_gpio_data *data = dev->data;
|
|
|
|
int64_t next_boot_ticks;
|
2022-01-06 20:17:39 +08:00
|
|
|
int rc = 0;
|
|
|
|
|
2022-03-12 13:40:35 +08:00
|
|
|
/* Validate that blocking API's can be used */
|
|
|
|
if (!k_can_yield()) {
|
|
|
|
LOG_ERR("Blocking actions cannot run in this context");
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2022-01-06 20:17:39 +08:00
|
|
|
switch (action) {
|
|
|
|
case PM_DEVICE_ACTION_RESUME:
|
2022-03-12 13:40:35 +08:00
|
|
|
/* Wait until we can boot again */
|
|
|
|
k_sleep(data->next_boot);
|
2022-01-06 20:17:39 +08:00
|
|
|
/* Switch power on */
|
|
|
|
gpio_pin_set_dt(&cfg->enable, 1);
|
2022-05-09 12:12:27 +08:00
|
|
|
LOG_INF("%s is now ON", dev->name);
|
2022-03-12 13:40:35 +08:00
|
|
|
/* Wait for domain to come up */
|
|
|
|
k_sleep(K_USEC(cfg->startup_delay_us));
|
2023-07-15 16:10:28 +08:00
|
|
|
#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
|
2023-01-22 10:32:15 +08:00
|
|
|
/* Notify devices on the domain they are now powered */
|
|
|
|
context.action = PM_DEVICE_ACTION_TURN_ON;
|
|
|
|
(void)device_supported_foreach(dev, pd_on_domain_visitor, &context);
|
2023-07-15 16:10:28 +08:00
|
|
|
#endif
|
2022-01-06 20:17:39 +08:00
|
|
|
break;
|
|
|
|
case PM_DEVICE_ACTION_SUSPEND:
|
2023-07-15 16:10:28 +08:00
|
|
|
#ifdef CONFIG_PM_DEVICE_POWER_DOMAIN
|
2023-01-22 10:32:15 +08:00
|
|
|
/* Notify devices on the domain that power is going down */
|
|
|
|
context.action = PM_DEVICE_ACTION_TURN_OFF;
|
|
|
|
(void)device_supported_foreach(dev, pd_on_domain_visitor, &context);
|
2023-07-15 16:10:28 +08:00
|
|
|
#endif
|
2022-01-06 20:17:39 +08:00
|
|
|
/* Switch power off */
|
|
|
|
gpio_pin_set_dt(&cfg->enable, 0);
|
2022-05-09 12:12:27 +08:00
|
|
|
LOG_INF("%s is now OFF", dev->name);
|
2022-03-12 13:40:35 +08:00
|
|
|
/* Store next time we can boot */
|
|
|
|
next_boot_ticks = k_uptime_ticks() + k_us_to_ticks_ceil32(cfg->off_on_delay_us);
|
|
|
|
data->next_boot = K_TIMEOUT_ABS_TICKS(next_boot_ticks);
|
2022-01-06 20:17:39 +08:00
|
|
|
break;
|
|
|
|
case PM_DEVICE_ACTION_TURN_ON:
|
|
|
|
/* Actively control the enable pin now that the device is powered */
|
|
|
|
gpio_pin_configure_dt(&cfg->enable, GPIO_OUTPUT_INACTIVE);
|
|
|
|
LOG_DBG("%s is OFF and powered", dev->name);
|
|
|
|
break;
|
|
|
|
case PM_DEVICE_ACTION_TURN_OFF:
|
|
|
|
/* Let the enable pin float while device is not powered */
|
|
|
|
gpio_pin_configure_dt(&cfg->enable, GPIO_DISCONNECTED);
|
|
|
|
LOG_DBG("%s is OFF and not powered", dev->name);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rc = -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pd_gpio_init(const struct device *dev)
|
|
|
|
{
|
|
|
|
const struct pd_gpio_config *cfg = dev->config;
|
2022-03-12 13:40:35 +08:00
|
|
|
struct pd_gpio_data *data = dev->data;
|
2022-01-06 20:17:39 +08:00
|
|
|
|
2023-08-26 11:43:58 +08:00
|
|
|
if (!gpio_is_ready_dt(&cfg->enable)) {
|
2022-01-06 20:17:39 +08:00
|
|
|
LOG_ERR("GPIO port %s is not ready", cfg->enable.port->name);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2022-03-12 13:40:35 +08:00
|
|
|
/* We can't know how long the domain has been off for before boot */
|
|
|
|
data->next_boot = K_TIMEOUT_ABS_US(cfg->off_on_delay_us);
|
2022-01-06 20:17:39 +08:00
|
|
|
|
2023-07-16 09:12:34 +08:00
|
|
|
/* Boot according to state */
|
|
|
|
return pm_device_driver_init(dev, pd_gpio_pm_action);
|
2022-01-06 20:17:39 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 21:57:26 +08:00
|
|
|
#define POWER_DOMAIN_DEVICE(id) \
|
|
|
|
static const struct pd_gpio_config pd_gpio_##id##_cfg = { \
|
|
|
|
.enable = GPIO_DT_SPEC_INST_GET(id, enable_gpios), \
|
|
|
|
.startup_delay_us = DT_INST_PROP(id, startup_delay_us), \
|
|
|
|
.off_on_delay_us = DT_INST_PROP(id, off_on_delay_us), \
|
|
|
|
}; \
|
|
|
|
static struct pd_gpio_data pd_gpio_##id##_data; \
|
|
|
|
PM_DEVICE_DT_INST_DEFINE(id, pd_gpio_pm_action); \
|
|
|
|
DEVICE_DT_INST_DEFINE(id, pd_gpio_init, PM_DEVICE_DT_INST_GET(id), \
|
|
|
|
&pd_gpio_##id##_data, &pd_gpio_##id##_cfg, \
|
2024-01-23 05:48:07 +08:00
|
|
|
POST_KERNEL, CONFIG_POWER_DOMAIN_GPIO_INIT_PRIORITY, \
|
2022-01-06 20:17:39 +08:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(POWER_DOMAIN_DEVICE)
|