From 8b0f6fd6dd3b799712a1791cb7c6c33017cd097b Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Wed, 13 Dec 2023 10:20:44 +0000 Subject: [PATCH] Add charging detection to vddh driver --- ...y_voltage_divider.h => battery_charging.h} | 4 +- .../drivers/sensor/battery/battery_common.c | 6 +-- .../drivers/sensor/battery/battery_nrf_vddh.c | 44 +++++++++++++++++-- .../sensor/battery/battery_voltage_divider.c | 2 +- .../bindings/sensor/zmk,battery-nrf-vddh.yaml | 6 +++ app/src/battery.c | 4 +- 6 files changed, 52 insertions(+), 14 deletions(-) rename app/include/drivers/sensor/battery/{battery_voltage_divider.h => battery_charging.h} (65%) diff --git a/app/include/drivers/sensor/battery/battery_voltage_divider.h b/app/include/drivers/sensor/battery/battery_charging.h similarity index 65% rename from app/include/drivers/sensor/battery/battery_voltage_divider.h rename to app/include/drivers/sensor/battery/battery_charging.h index 5324097c6202..aa5075216907 100644 --- a/app/include/drivers/sensor/battery/battery_voltage_divider.h +++ b/app/include/drivers/sensor/battery/battery_charging.h @@ -4,8 +4,8 @@ * SPDX-License-Identifier: MIT */ -#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_BATTERY_VOLTAGE_DIVIDER_H_ -#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_BATTERY_VOLTAGE_DIVIDER_H_ +#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_BATTERY_CHARGING_H_ +#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_BATTERY_BATTERY_CHARGING_H_ #ifdef __cplusplus extern "C" { diff --git a/app/module/drivers/sensor/battery/battery_common.c b/app/module/drivers/sensor/battery/battery_common.c index 6cef58145b6c..4a2f3b2048f9 100644 --- a/app/module/drivers/sensor/battery/battery_common.c +++ b/app/module/drivers/sensor/battery/battery_common.c @@ -7,9 +7,7 @@ #include #include -#if CONFIG_ZMK_BATTERY_VOLTAGE_DIVIDER -#include -#endif +#include #include "battery_common.h" @@ -26,12 +24,10 @@ int battery_channel_get(const struct battery_value *value, enum sensor_channel c val_out->val2 = 0; break; -#if CONFIG_ZMK_BATTERY_VOLTAGE_DIVIDER case SENSOR_CHAN_CHARGING: val_out->val1 = value->charging; val_out->val2 = 0; break; -#endif default: return -ENOTSUP; diff --git a/app/module/drivers/sensor/battery/battery_nrf_vddh.c b/app/module/drivers/sensor/battery/battery_nrf_vddh.c index 32c7c61eb825..05cdea712748 100644 --- a/app/module/drivers/sensor/battery/battery_nrf_vddh.c +++ b/app/module/drivers/sensor/battery/battery_nrf_vddh.c @@ -11,10 +11,12 @@ #include #include +#include #include #include #include +#include #include "battery_common.h" LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -23,6 +25,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); static const struct device *adc = DEVICE_DT_GET(DT_NODELABEL(adc)); +struct vddh_config { + struct gpio_dt_spec chg; +}; + struct vddh_data { struct adc_channel_cfg acc; struct adc_sequence as; @@ -32,12 +38,13 @@ struct vddh_data { static int vddh_sample_fetch(const struct device *dev, enum sensor_channel chan) { // Make sure selected channel is supported if (chan != SENSOR_CHAN_GAUGE_VOLTAGE && chan != SENSOR_CHAN_GAUGE_STATE_OF_CHARGE && - chan != SENSOR_CHAN_ALL) { + (enum sensor_channel_bvd)chan != SENSOR_CHAN_CHARGING && chan != SENSOR_CHAN_ALL) { LOG_DBG("Selected channel is not supported: %d.", chan); return -ENOTSUP; } struct vddh_data *drv_data = dev->data; + const struct vddh_config *drv_cfg = dev->config; struct adc_sequence *as = &drv_data->as; int rc = adc_read(adc, as); @@ -61,6 +68,18 @@ static int vddh_sample_fetch(const struct device *dev, enum sensor_channel chan) LOG_DBG("ADC raw %d ~ %d mV => %d%%", drv_data->value.adc_raw, drv_data->value.millivolts, drv_data->value.state_of_charge); +#if DT_INST_NODE_HAS_PROP(0, chg_gpios) + int raw = gpio_pin_get_dt(&drv_cfg->chg); + if (raw == -EIO || raw == -EWOULDBLOCK) { + LOG_DBG("Failed to read chg status: %d", raw); + return raw; + } else { + bool charging = raw; + LOG_DBG("Charging state: %d", raw); + drv_data->value.charging = charging; + } +#endif + return rc; } @@ -77,6 +96,7 @@ static const struct sensor_driver_api vddh_api = { static int vddh_init(const struct device *dev) { struct vddh_data *drv_data = dev->data; + const struct vddh_config *drv_cfg = dev->config; if (!device_is_ready(adc)) { LOG_ERR("ADC device is not ready %s", adc->name); @@ -104,13 +124,31 @@ static int vddh_init(const struct device *dev) { #error Unsupported ADC #endif - const int rc = adc_channel_setup(adc, &drv_data->acc); + int rc = adc_channel_setup(adc, &drv_data->acc); LOG_DBG("VDDHDIV5 setup returned %d", rc); +#if DT_INST_NODE_HAS_PROP(0, chg_gpios) + if (!device_is_ready(drv_cfg->chg.port)) { + LOG_ERR("GPIO port for chg reading is not ready"); + return -ENODEV; + } + rc = gpio_pin_configure_dt(&drv_cfg->chg, GPIO_INPUT); + if (rc != 0) { + LOG_ERR("Failed to set chg feed %u: %d", drv_cfg->chg.pin, rc); + return rc; + } +#endif // DT_INST_NODE_HAS_PROP(0, chg_gpios) + return rc; } static struct vddh_data vddh_data; -DEVICE_DT_INST_DEFINE(0, &vddh_init, NULL, &vddh_data, NULL, POST_KERNEL, +static const struct vddh_config vddh_cfg = { +#if DT_INST_NODE_HAS_PROP(0, chg_gpios) + .chg = GPIO_DT_SPEC_INST_GET(0, chg_gpios), +#endif +}; + +DEVICE_DT_INST_DEFINE(0, &vddh_init, NULL, &vddh_data, &vddh_cfg, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &vddh_api); diff --git a/app/module/drivers/sensor/battery/battery_voltage_divider.c b/app/module/drivers/sensor/battery/battery_voltage_divider.c index 3445d0827090..881449ff3950 100644 --- a/app/module/drivers/sensor/battery/battery_voltage_divider.c +++ b/app/module/drivers/sensor/battery/battery_voltage_divider.c @@ -14,7 +14,7 @@ #include #include "battery_common.h" -#include +#include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); diff --git a/app/module/dts/bindings/sensor/zmk,battery-nrf-vddh.yaml b/app/module/dts/bindings/sensor/zmk,battery-nrf-vddh.yaml index 28b7541b8199..f1ce9a461f83 100644 --- a/app/module/dts/bindings/sensor/zmk,battery-nrf-vddh.yaml +++ b/app/module/dts/bindings/sensor/zmk,battery-nrf-vddh.yaml @@ -4,3 +4,9 @@ description: Battery SoC monitoring using nRF VDDH compatible: "zmk,battery-nrf-vddh" + +properties: + chg-gpios: + required: false + type: phandle-array + description: "A GPIO pin to report charging state to" diff --git a/app/src/battery.c b/app/src/battery.c index 139181598b49..7fa3b4039f1b 100644 --- a/app/src/battery.c +++ b/app/src/battery.c @@ -22,9 +22,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include -#if CONFIG_ZMK_BATTERY_VOLTAGE_DIVIDER -#include -#endif +#include static uint8_t last_state_of_charge = 0; static bool charging = 0;