197 lines
5.1 KiB
C
197 lines
5.1 KiB
C
|
|
#include <errno.h>
|
||
|
|
#include <stdint.h>
|
||
|
|
|
||
|
|
#include <zephyr/device.h>
|
||
|
|
#include <zephyr/devicetree.h>
|
||
|
|
#include <zephyr/drivers/gpio.h>
|
||
|
|
#include <zephyr/drivers/i2c.h>
|
||
|
|
#include <zephyr/kernel.h>
|
||
|
|
#include <zephyr/logging/log.h>
|
||
|
|
#include <zephyr/sys/util.h>
|
||
|
|
|
||
|
|
#include <drivers/pmic/ip5306.h>
|
||
|
|
|
||
|
|
#define DT_DRV_COMPAT injoinic_ip5306
|
||
|
|
|
||
|
|
LOG_MODULE_REGISTER(ip5306, LOG_LEVEL_INF);
|
||
|
|
|
||
|
|
#define IP5306_REG_READ0 0x70
|
||
|
|
#define IP5306_REG_READ1 0x71
|
||
|
|
#define IP5306_CHARGING_BIT BIT(3)
|
||
|
|
#define IP5306_FULL_BIT BIT(3)
|
||
|
|
|
||
|
|
struct ip5306_config {
|
||
|
|
struct i2c_dt_spec bus;
|
||
|
|
struct gpio_dt_spec wakeup_gpio;
|
||
|
|
uint32_t keepalive_interval_ms;
|
||
|
|
uint32_t keepalive_pulse_width_ms;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct ip5306_data {
|
||
|
|
const struct device *dev;
|
||
|
|
struct k_work_delayable keepalive_work;
|
||
|
|
bool started;
|
||
|
|
bool pulse_active;
|
||
|
|
};
|
||
|
|
|
||
|
|
static uint32_t keepalive_low_delay_ms(const struct ip5306_config *config)
|
||
|
|
{
|
||
|
|
if (config->keepalive_interval_ms > config->keepalive_pulse_width_ms) {
|
||
|
|
return config->keepalive_interval_ms -
|
||
|
|
config->keepalive_pulse_width_ms;
|
||
|
|
}
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void keepalive_work_handler(struct k_work *work)
|
||
|
|
{
|
||
|
|
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
|
||
|
|
struct ip5306_data *data =
|
||
|
|
CONTAINER_OF(dwork, struct ip5306_data, keepalive_work);
|
||
|
|
const struct device *dev = data->dev;
|
||
|
|
const struct ip5306_config *config = dev->config;
|
||
|
|
int err;
|
||
|
|
|
||
|
|
if (!data->started) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!data->pulse_active) {
|
||
|
|
err = gpio_pin_set_dt(&config->wakeup_gpio, 1);
|
||
|
|
if (err) {
|
||
|
|
LOG_ERR("Failed to assert wakeup pulse (%d)", err);
|
||
|
|
}
|
||
|
|
|
||
|
|
data->pulse_active = true;
|
||
|
|
k_work_reschedule(&data->keepalive_work,
|
||
|
|
K_MSEC(config->keepalive_pulse_width_ms));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
err = gpio_pin_set_dt(&config->wakeup_gpio, 0);
|
||
|
|
if (err) {
|
||
|
|
LOG_ERR("Failed to deassert wakeup pulse (%d)", err);
|
||
|
|
}
|
||
|
|
|
||
|
|
data->pulse_active = false;
|
||
|
|
k_work_reschedule(&data->keepalive_work,
|
||
|
|
K_MSEC(keepalive_low_delay_ms(config)));
|
||
|
|
}
|
||
|
|
|
||
|
|
static int ip5306_init_api(const struct device *dev)
|
||
|
|
{
|
||
|
|
const struct ip5306_config *config = dev->config;
|
||
|
|
struct ip5306_data *data = dev->data;
|
||
|
|
uint8_t reg_val;
|
||
|
|
int err;
|
||
|
|
|
||
|
|
if (data->started) {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
err = i2c_reg_read_byte_dt(&config->bus, IP5306_REG_READ0, ®_val);
|
||
|
|
if (err) {
|
||
|
|
LOG_ERR("IP5306 probe failed (%d)", err);
|
||
|
|
return err;
|
||
|
|
}
|
||
|
|
|
||
|
|
data->started = true;
|
||
|
|
data->pulse_active = false;
|
||
|
|
k_work_reschedule(&data->keepalive_work,
|
||
|
|
K_MSEC(keepalive_low_delay_ms(config)));
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int ip5306_get_status_api(const struct device *dev,
|
||
|
|
struct ip5306_status *status)
|
||
|
|
{
|
||
|
|
const struct ip5306_config *config = dev->config;
|
||
|
|
uint8_t read0;
|
||
|
|
uint8_t read1;
|
||
|
|
int err;
|
||
|
|
|
||
|
|
if (status == NULL) {
|
||
|
|
return -EINVAL;
|
||
|
|
}
|
||
|
|
|
||
|
|
err = i2c_reg_read_byte_dt(&config->bus, IP5306_REG_READ0, &read0);
|
||
|
|
if (err) {
|
||
|
|
return err;
|
||
|
|
}
|
||
|
|
|
||
|
|
err = i2c_reg_read_byte_dt(&config->bus, IP5306_REG_READ1, &read1);
|
||
|
|
if (err) {
|
||
|
|
return err;
|
||
|
|
}
|
||
|
|
|
||
|
|
status->charging = (read0 & IP5306_CHARGING_BIT) != 0U;
|
||
|
|
status->full = (read1 & IP5306_FULL_BIT) != 0U;
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int ip5306_dev_init(const struct device *dev)
|
||
|
|
{
|
||
|
|
const struct ip5306_config *config = dev->config;
|
||
|
|
struct ip5306_data *data = dev->data;
|
||
|
|
|
||
|
|
if (!i2c_is_ready_dt(&config->bus)) {
|
||
|
|
LOG_ERR("I2C bus not ready");
|
||
|
|
return -ENODEV;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!gpio_is_ready_dt(&config->wakeup_gpio)) {
|
||
|
|
LOG_ERR("Wakeup GPIO not ready");
|
||
|
|
return -ENODEV;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (config->keepalive_pulse_width_ms == 0U) {
|
||
|
|
LOG_ERR("Invalid keepalive pulse width");
|
||
|
|
return -EINVAL;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (config->keepalive_interval_ms == 0U) {
|
||
|
|
LOG_ERR("Invalid keepalive interval");
|
||
|
|
return -EINVAL;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (config->keepalive_pulse_width_ms > config->keepalive_interval_ms) {
|
||
|
|
LOG_ERR("Pulse width cannot exceed interval");
|
||
|
|
return -EINVAL;
|
||
|
|
}
|
||
|
|
|
||
|
|
data->dev = dev;
|
||
|
|
data->started = false;
|
||
|
|
data->pulse_active = false;
|
||
|
|
|
||
|
|
k_work_init_delayable(&data->keepalive_work, keepalive_work_handler);
|
||
|
|
|
||
|
|
return gpio_pin_configure_dt(&config->wakeup_gpio, GPIO_OUTPUT_INACTIVE);
|
||
|
|
}
|
||
|
|
|
||
|
|
static const struct ip5306_driver_api ip5306_driver_api = {
|
||
|
|
.init = ip5306_init_api,
|
||
|
|
.get_status = ip5306_get_status_api,
|
||
|
|
};
|
||
|
|
|
||
|
|
#define IP5306_DEFINE(inst) \
|
||
|
|
static struct ip5306_data ip5306_data_##inst; \
|
||
|
|
\
|
||
|
|
static const struct ip5306_config ip5306_config_##inst = { \
|
||
|
|
.bus = I2C_DT_SPEC_INST_GET(inst), \
|
||
|
|
.wakeup_gpio = GPIO_DT_SPEC_INST_GET(inst, wakeup_gpios), \
|
||
|
|
.keepalive_interval_ms = \
|
||
|
|
DT_INST_PROP(inst, keepalive_interval_ms), \
|
||
|
|
.keepalive_pulse_width_ms = \
|
||
|
|
DT_INST_PROP(inst, keepalive_pulse_width_ms), \
|
||
|
|
}; \
|
||
|
|
\
|
||
|
|
DEVICE_DT_INST_DEFINE(inst, ip5306_dev_init, NULL, \
|
||
|
|
&ip5306_data_##inst, &ip5306_config_##inst, \
|
||
|
|
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
|
||
|
|
&ip5306_driver_api)
|
||
|
|
|
||
|
|
DT_INST_FOREACH_STATUS_OKAY(IP5306_DEFINE)
|