New kernel drivers and device migrations (#563)

This commit is contained in:
Ken Van Hoeylandt
2026-07-14 20:26:57 +02:00
committed by GitHub
parent 955416dac8
commit 8af6204ba1
215 changed files with 6359 additions and 3633 deletions
@@ -0,0 +1,119 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/drivers/esp32_gpio_backlight.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/backlight.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/gpio_descriptor.h>
#include <tactility/log.h>
#include <cstdlib>
#define TAG "Esp32GpioBacklight"
#define GET_CONFIG(device) (static_cast<const Esp32GpioBacklightConfig*>((device)->config))
struct Esp32GpioBacklightInternal {
GpioDescriptor* descriptor;
uint8_t brightness;
};
// region Driver lifecycle
static error_t start(Device* device) {
const auto* config = GET_CONFIG(device);
auto* descriptor = gpio_descriptor_acquire(config->pin_backlight.gpio_controller, config->pin_backlight.pin, GPIO_OWNER_GPIO);
if (descriptor == nullptr) {
LOG_E(TAG, "Failed to acquire GPIO descriptor");
return ERROR_RESOURCE;
}
if (gpio_descriptor_set_flags(descriptor, config->pin_backlight.flags | GPIO_FLAG_DIRECTION_OUTPUT) != ERROR_NONE) {
LOG_E(TAG, "Failed to configure backlight pin as output");
gpio_descriptor_release(descriptor);
return ERROR_RESOURCE;
}
auto* internal = static_cast<Esp32GpioBacklightInternal*>(malloc(sizeof(Esp32GpioBacklightInternal)));
if (internal == nullptr) {
gpio_descriptor_release(descriptor);
return ERROR_OUT_OF_MEMORY;
}
internal->descriptor = descriptor;
internal->brightness = 0;
device_set_driver_data(device, internal);
backlight_set_brightness_default(device); // Allowed to fail, we don't care about the result
return ERROR_NONE;
}
static error_t stop(Device* device) {
backlight_set_brightness(device, 0); // Allowed to fail, we don't care about the result
auto* internal = static_cast<Esp32GpioBacklightInternal*>(device_get_driver_data(device));
gpio_descriptor_release(internal->descriptor);
free(internal);
return ERROR_NONE;
}
// endregion
// region BacklightApi
static error_t esp32_gpio_backlight_set_brightness(Device* device, uint8_t brightness) {
auto* internal = static_cast<Esp32GpioBacklightInternal*>(device_get_driver_data(device));
error_t error = gpio_descriptor_set_level(internal->descriptor, brightness > 0);
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to set backlight level");
return error;
}
internal->brightness = brightness;
return ERROR_NONE;
}
static error_t esp32_gpio_backlight_set_brightness_default(Device* device) {
return esp32_gpio_backlight_set_brightness(device, GET_CONFIG(device)->default_on ? 1 : 0);
}
static error_t esp32_gpio_backlight_get_brightness(Device* device, uint8_t* out_brightness) {
auto* internal = static_cast<Esp32GpioBacklightInternal*>(device_get_driver_data(device));
*out_brightness = internal->brightness;
return ERROR_NONE;
}
static uint8_t esp32_gpio_backlight_get_min_brightness(Device*) {
return 0;
}
static uint8_t esp32_gpio_backlight_get_max_brightness(Device*) {
return 1;
}
// endregion
static const BacklightApi esp32_gpio_backlight_api = {
.set_brightness = esp32_gpio_backlight_set_brightness,
.set_brightness_default = esp32_gpio_backlight_set_brightness_default,
.get_brightness = esp32_gpio_backlight_get_brightness,
.get_min_brightness = esp32_gpio_backlight_get_min_brightness,
.get_max_brightness = esp32_gpio_backlight_get_max_brightness,
};
extern Module platform_esp32_module;
Driver esp32_gpio_backlight_driver = {
.name = "esp32_gpio_backlight",
.compatible = (const char*[]) { "espressif,esp32-gpio-backlight", nullptr },
.start_device = start,
.stop_device = stop,
.api = &esp32_gpio_backlight_api,
.device_type = &BACKLIGHT_TYPE,
.owner = &platform_esp32_module,
.internal = nullptr
};
@@ -0,0 +1,192 @@
// SPDX-License-Identifier: Apache-2.0
#include <soc/soc_caps.h>
#if SOC_LCD_I80_SUPPORTED
#include <tactility/device.h>
#include <tactility/drivers/esp32_i8080.h>
#include <tactility/log.h>
#include "tactility/drivers/gpio_descriptor.h"
#include <tactility/drivers/esp32_gpio_helpers.h>
#include <tactility/drivers/gpio_controller.h>
#include <esp_lcd_panel_io.h>
#include <new>
#include <vector>
#define TAG "esp32_i8080"
#define GET_CONFIG(device) ((const struct Esp32I8080Config*)device->config)
#define GET_DATA(device) ((struct Esp32I8080Internal*)device_get_driver_data(device))
extern "C" {
struct Esp32I8080Internal {
esp_lcd_i80_bus_handle_t bus_handle = nullptr;
GpioDescriptor* dc_descriptor = nullptr;
GpioDescriptor* wr_descriptor = nullptr;
GpioDescriptor* rd_descriptor = nullptr;
GpioDescriptor* d0_descriptor = nullptr;
GpioDescriptor* d1_descriptor = nullptr;
GpioDescriptor* d2_descriptor = nullptr;
GpioDescriptor* d3_descriptor = nullptr;
GpioDescriptor* d4_descriptor = nullptr;
GpioDescriptor* d5_descriptor = nullptr;
GpioDescriptor* d6_descriptor = nullptr;
GpioDescriptor* d7_descriptor = nullptr;
std::vector<GpioDescriptor*> cs_descriptors;
void cleanup_pins() {
release_pin(&dc_descriptor);
release_pin(&wr_descriptor);
release_pin(&rd_descriptor);
release_pin(&d0_descriptor);
release_pin(&d1_descriptor);
release_pin(&d2_descriptor);
release_pin(&d3_descriptor);
release_pin(&d4_descriptor);
release_pin(&d5_descriptor);
release_pin(&d6_descriptor);
release_pin(&d7_descriptor);
for (auto*& desc : cs_descriptors) {
release_pin(&desc);
}
cs_descriptors.clear();
}
};
static error_t start(Device* device) {
LOG_I(TAG, "start %s", device->name);
auto* data = new (std::nothrow) Esp32I8080Internal();
if (data == nullptr) return ERROR_OUT_OF_MEMORY;
device_set_driver_data(device, data);
const auto* config = GET_CONFIG(device);
bool pins_ok =
acquire_pin_or_set_null(config->pin_dc, &data->dc_descriptor) &&
acquire_pin_or_set_null(config->pin_wr, &data->wr_descriptor) &&
acquire_pin_or_set_null(config->pin_rd, &data->rd_descriptor) &&
acquire_pin_or_set_null(config->pin_d0, &data->d0_descriptor) &&
acquire_pin_or_set_null(config->pin_d1, &data->d1_descriptor) &&
acquire_pin_or_set_null(config->pin_d2, &data->d2_descriptor) &&
acquire_pin_or_set_null(config->pin_d3, &data->d3_descriptor) &&
acquire_pin_or_set_null(config->pin_d4, &data->d4_descriptor) &&
acquire_pin_or_set_null(config->pin_d5, &data->d5_descriptor) &&
acquire_pin_or_set_null(config->pin_d6, &data->d6_descriptor) &&
acquire_pin_or_set_null(config->pin_d7, &data->d7_descriptor);
if (!pins_ok) {
LOG_E(TAG, "Failed to acquire required i8080 pins");
data->cleanup_pins();
device_set_driver_data(device, nullptr);
delete data;
return ERROR_RESOURCE;
}
// RD is never toggled by the i80 LCD peripheral (write-only bus); drive it high once so the
// panel doesn't see spurious read strobes.
if (data->rd_descriptor != nullptr) {
gpio_descriptor_set_flags(data->rd_descriptor, GPIO_FLAG_DIRECTION_OUTPUT);
gpio_descriptor_set_level(data->rd_descriptor, true);
}
esp_lcd_i80_bus_config_t bus_cfg = {
.dc_gpio_num = get_native_pin(data->dc_descriptor),
.wr_gpio_num = get_native_pin(data->wr_descriptor),
.clk_src = LCD_CLK_SRC_DEFAULT,
.data_gpio_nums = {
get_native_pin(data->d0_descriptor),
get_native_pin(data->d1_descriptor),
get_native_pin(data->d2_descriptor),
get_native_pin(data->d3_descriptor),
get_native_pin(data->d4_descriptor),
get_native_pin(data->d5_descriptor),
get_native_pin(data->d6_descriptor),
get_native_pin(data->d7_descriptor),
},
.bus_width = 8,
.max_transfer_bytes = static_cast<size_t>(config->max_transfer_bytes),
.psram_trans_align = 64,
.sram_trans_align = 4
};
esp_err_t ret = esp_lcd_new_i80_bus(&bus_cfg, &data->bus_handle);
if (ret != ESP_OK) {
LOG_E(TAG, "Failed to create I80 bus: %s", esp_err_to_name(ret));
data->cleanup_pins();
device_set_driver_data(device, nullptr);
delete data;
return ERROR_RESOURCE;
}
// Acquire and deselect all CS pins (drive high)
for (uint8_t i = 0; i < config->cs_gpios_count; i++) {
const GpioPinSpec* cs = &config->cs_gpios[i];
if (cs->gpio_controller == nullptr) continue;
GpioDescriptor* desc = gpio_descriptor_acquire(cs->gpio_controller, cs->pin, GPIO_OWNER_GPIO);
if (desc != nullptr) {
gpio_descriptor_set_flags(desc, GPIO_FLAG_DIRECTION_OUTPUT);
gpio_descriptor_set_level(desc, true);
data->cs_descriptors.push_back(desc);
} else {
LOG_E(TAG, "Failed to acquire CS pin %u", i);
}
}
return ERROR_NONE;
}
static error_t stop(Device* device) {
LOG_I(TAG, "stop %s", device->name);
auto* data = GET_DATA(device);
if (data->bus_handle != nullptr) {
esp_lcd_del_i80_bus(data->bus_handle);
}
data->cleanup_pins();
device_set_driver_data(device, nullptr);
delete data;
return ERROR_NONE;
}
error_t esp32_i8080_get_cs_pin(Device* child_device, GpioPinSpec* out_pin) {
auto* parent = device_get_parent(child_device);
if (parent == nullptr || device_get_type(parent) != &I8080_CONTROLLER_TYPE) return ERROR_INVALID_STATE;
const auto* config = GET_CONFIG(parent);
int32_t index = child_device->address;
if (index < 0 || index >= config->cs_gpios_count) return ERROR_OUT_OF_RANGE;
*out_pin = config->cs_gpios[index];
return ERROR_NONE;
}
error_t esp32_i8080_get_bus_handle(Device* child_device, esp_lcd_i80_bus_handle_t* out_handle) {
auto* parent = device_get_parent(child_device);
if (parent == nullptr || device_get_type(parent) != &I8080_CONTROLLER_TYPE) return ERROR_INVALID_STATE;
auto* data = GET_DATA(parent);
if (data == nullptr || data->bus_handle == nullptr) return ERROR_INVALID_STATE;
*out_handle = data->bus_handle;
return ERROR_NONE;
}
extern struct Module platform_esp32_module;
Driver esp32_i8080_driver = {
.name = "esp32_i8080",
.compatible = (const char*[]) { "espressif,esp32-i8080", nullptr },
.start_device = start,
.stop_device = stop,
.api = nullptr,
.device_type = &I8080_CONTROLLER_TYPE,
.owner = &platform_esp32_module,
.internal = nullptr
};
} // extern "C"
#endif // SOC_LCD_I80_SUPPORTED
@@ -16,6 +16,10 @@ extern Driver esp32_gpio_driver;
extern Driver esp32_i2c_driver;
extern Driver esp32_i2c_master_driver;
extern Driver esp32_i2s_driver;
#if SOC_LCD_I80_SUPPORTED
extern Driver esp32_i8080_driver;
#endif
extern Driver esp32_gpio_backlight_driver;
extern Driver esp32_ledc_backlight_driver;
#if SOC_SDMMC_HOST_SUPPORTED
extern Driver esp32_sdmmc_driver;
@@ -49,6 +53,10 @@ static error_t start() {
check(driver_construct_add(&esp32_i2c_driver) == ERROR_NONE);
check(driver_construct_add(&esp32_i2c_master_driver) == ERROR_NONE);
check(driver_construct_add(&esp32_i2s_driver) == ERROR_NONE);
#if SOC_LCD_I80_SUPPORTED
check(driver_construct_add(&esp32_i8080_driver) == ERROR_NONE);
#endif
check(driver_construct_add(&esp32_gpio_backlight_driver) == ERROR_NONE);
check(driver_construct_add(&esp32_ledc_backlight_driver) == ERROR_NONE);
#if SOC_SDMMC_HOST_SUPPORTED
check(driver_construct_add(&esp32_sdmmc_driver) == ERROR_NONE);
@@ -100,7 +108,11 @@ static error_t stop() {
check(driver_remove_destruct(&esp32_i2c_driver) == ERROR_NONE);
check(driver_remove_destruct(&esp32_i2c_master_driver) == ERROR_NONE);
check(driver_remove_destruct(&esp32_i2s_driver) == ERROR_NONE);
#if SOC_LCD_I80_SUPPORTED
check(driver_remove_destruct(&esp32_i8080_driver) == ERROR_NONE);
#endif
check(driver_remove_destruct(&esp32_ledc_backlight_driver) == ERROR_NONE);
check(driver_remove_destruct(&esp32_gpio_backlight_driver) == ERROR_NONE);
#if SOC_SDMMC_HOST_SUPPORTED
check(driver_remove_destruct(&esp32_sdmmc_driver) == ERROR_NONE);
#endif