Device migrations, driver migrations and more (#575)

This commit is contained in:
Ken Van Hoeylandt
2026-07-20 23:43:17 +02:00
committed by GitHub
parent 2d768ef3a1
commit 2fbc44466a
221 changed files with 8824 additions and 3652 deletions
+150 -1
View File
@@ -1,11 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
#include <drivers/ina226.h>
#include <ina226_module.h>
#include <tactility/check.h>
#include <tactility/device.h>
#include <tactility/drivers/i2c_controller.h>
#include <tactility/drivers/power_supply.h>
#include <tactility/log.h>
#define TAG "INA226"
#include <cstdlib>
#include <new>
constexpr auto* TAG = "INA226";
// ---------------------------------------------------------------------------
// Register map
@@ -30,6 +35,124 @@ static constexpr TickType_t TIMEOUT = pdMS_TO_TICKS(50);
#define GET_CONFIG(device) (static_cast<const Ina226Config*>((device)->config))
struct Ina226Internal {
Device* power_supply_device;
};
// ---------------------------------------------------------------------------
// Power supply
// ---------------------------------------------------------------------------
static bool ps_supports_property(Device*, PowerSupplyProperty property) {
return property == POWER_SUPPLY_PROP_VOLTAGE || property == POWER_SUPPLY_PROP_CAPACITY;
}
// battery_mv <= min_mv -> 0%, battery_mv >= reference_mv -> 100%, linear in between. min_mv is
// configurable (power-supply-min-voltage-mv) since it depends on the pack's cell count - a 1S
// LiPo's empty floor is very different from e.g. a 2S pack's.
static int estimate_capacity_from_mv(int battery_mv, uint32_t min_mv, uint32_t reference_mv) {
if ((uint32_t)battery_mv <= min_mv) return 0;
if ((uint32_t)battery_mv >= reference_mv) return 100;
return (battery_mv - (int)min_mv) * 100 / ((int)reference_mv - (int)min_mv);
}
static error_t ps_get_property(Device* device, PowerSupplyProperty property, PowerSupplyPropertyValue* out_value) {
if (property != POWER_SUPPLY_PROP_VOLTAGE && property != POWER_SUPPLY_PROP_CAPACITY) {
return ERROR_NOT_SUPPORTED;
}
auto* parent = device_get_parent(device);
float volts;
error_t error = ina226_read_bus_voltage(parent, &volts);
if (error != ERROR_NONE) {
return error;
}
int battery_mv = static_cast<int>(volts * 1000.0f);
const auto* parent_config = GET_CONFIG(parent);
out_value->int_value = (property == POWER_SUPPLY_PROP_VOLTAGE)
? battery_mv
: estimate_capacity_from_mv(battery_mv, parent_config->power_supply_min_voltage_mv, parent_config->power_supply_reference_voltage_mv);
return ERROR_NONE;
}
static bool ps_supports_charge_control(Device*) { return false; }
static bool ps_is_allowed_to_charge(Device*) { return false; }
static error_t ps_set_allowed_to_charge(Device*, bool) { return ERROR_NOT_SUPPORTED; }
static bool ps_supports_quick_charge(Device*) { return false; }
static bool ps_is_quick_charge_enabled(Device*) { return false; }
static error_t ps_set_quick_charge_enabled(Device*, bool) { return ERROR_NOT_SUPPORTED; }
static bool ps_supports_power_off(Device*) { return false; }
static error_t ps_power_off(Device*) { return ERROR_NOT_SUPPORTED; }
static constexpr PowerSupplyApi INA226_POWER_SUPPLY_API = {
.supports_property = ps_supports_property,
.get_property = ps_get_property,
.supports_charge_control = ps_supports_charge_control,
.is_allowed_to_charge = ps_is_allowed_to_charge,
.set_allowed_to_charge = ps_set_allowed_to_charge,
.supports_quick_charge = ps_supports_quick_charge,
.is_quick_charge_enabled = ps_is_quick_charge_enabled,
.set_quick_charge_enabled = ps_set_quick_charge_enabled,
.supports_power_off = ps_supports_power_off,
.power_off = ps_power_off,
};
// Registered (driver_construct_add() in module.cpp) so driver_bind() has a valid ->internal,
// but never matched against a devicetree node: ina226 wires it up directly by pointer.
Driver ina226_power_supply_driver = {
.name = "ina226-power-supply",
.compatible = (const char*[]) { "ina226-power-supply", nullptr },
.start_device = nullptr,
.stop_device = nullptr,
.api = &INA226_POWER_SUPPLY_API,
.device_type = &POWER_SUPPLY_TYPE,
.owner = &ina226_module,
.internal = nullptr
};
static error_t create_power_supply_child(Device* parent, Device*& out_child) {
auto* child = new(std::nothrow) Device { .address = 0, .name = "ina226-power-supply", .config = nullptr, .parent = nullptr, .internal = nullptr };
if (child == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
error_t error = device_construct(child);
if (error != ERROR_NONE) {
delete child;
return error;
}
device_set_parent(child, parent);
device_set_driver(child, &ina226_power_supply_driver);
error = device_add(child);
if (error != ERROR_NONE) {
device_destruct(child);
delete child;
return error;
}
error = device_start(child);
if (error != ERROR_NONE) {
device_remove(child);
device_destruct(child);
delete child;
return error;
}
out_child = child;
return ERROR_NONE;
}
static void destroy_power_supply_child(Device* child) {
check(device_stop(child) == ERROR_NONE);
check(device_remove(child) == ERROR_NONE);
check(device_destruct(child) == ERROR_NONE);
delete child;
}
// ---------------------------------------------------------------------------
// Driver lifecycle
// ---------------------------------------------------------------------------
@@ -77,6 +200,23 @@ static error_t start(Device* device) {
return ERROR_RESOURCE;
}
auto* internal = static_cast<Ina226Internal*>(malloc(sizeof(Ina226Internal)));
if (internal == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
internal->power_supply_device = nullptr;
device_set_driver_data(device, internal);
if (GET_CONFIG(device)->power_supply) {
error_t error = create_power_supply_child(device, internal->power_supply_device);
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to create power-supply device");
free(internal);
device_set_driver_data(device, nullptr);
return error;
}
}
LOG_I(TAG, "INA226 started (addr=0x%02X, shunt=%umΩ, cal=0x%04X)", addr, GET_CONFIG(device)->shunt_milliohms, cal_value);
return ERROR_NONE;
}
@@ -88,6 +228,12 @@ static error_t stop(Device* device) {
return ERROR_RESOURCE;
}
auto* internal = static_cast<Ina226Internal*>(device_get_driver_data(device));
if (internal->power_supply_device != nullptr) {
destroy_power_supply_child(internal->power_supply_device);
internal->power_supply_device = nullptr;
}
auto addr = GET_CONFIG(device)->address;
// Put device into shutdown mode to save power
@@ -95,6 +241,9 @@ static error_t stop(Device* device) {
LOG_E(TAG, "Failed to shutdown INA226 (ignored)");
}
free(internal);
device_set_driver_data(device, nullptr);
return ERROR_NONE;
}