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
+11
View File
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.20)
include("${CMAKE_CURRENT_LIST_DIR}/../../Buildscripts/module.cmake")
file(GLOB_RECURSE SOURCE_FILES "source/*.c*")
tactility_add_module(bq24295-module
SRCS ${SOURCE_FILES}
INCLUDE_DIRS include/
REQUIRES TactilityKernel
)
@@ -0,0 +1,195 @@
Apache License
==============
_Version 2.0, January 2004_
_&lt;<http://www.apache.org/licenses/>&gt;_
### Terms and Conditions for use, reproduction, and distribution
#### 1. Definitions
“License” shall mean the terms and conditions for use, reproduction, and
distribution as defined by Sections 1 through 9 of this document.
“Licensor” shall mean the copyright owner or entity authorized by the copyright
owner that is granting the License.
“Legal Entity” shall mean the union of the acting entity and all other entities
that control, are controlled by, or are under common control with that entity.
For the purposes of this definition, “control” means **(i)** the power, direct or
indirect, to cause the direction or management of such entity, whether by
contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the
outstanding shares, or **(iii)** beneficial ownership of such entity.
“You” (or “Your”) shall mean an individual or Legal Entity exercising
permissions granted by this License.
“Source” form shall mean the preferred form for making modifications, including
but not limited to software source code, documentation source, and configuration
files.
“Object” form shall mean any form resulting from mechanical transformation or
translation of a Source form, including but not limited to compiled object code,
generated documentation, and conversions to other media types.
“Work” shall mean the work of authorship, whether in Source or Object form, made
available under the License, as indicated by a copyright notice that is included
in or attached to the work (an example is provided in the Appendix below).
“Derivative Works” shall mean any work, whether in Source or Object form, that
is based on (or derived from) the Work and for which the editorial revisions,
annotations, elaborations, or other modifications represent, as a whole, an
original work of authorship. For the purposes of this License, Derivative Works
shall not include works that remain separable from, or merely link (or bind by
name) to the interfaces of, the Work and Derivative Works thereof.
“Contribution” shall mean any work of authorship, including the original version
of the Work and any modifications or additions to that Work or Derivative Works
thereof, that is intentionally submitted to Licensor for inclusion in the Work
by the copyright owner or by an individual or Legal Entity authorized to submit
on behalf of the copyright owner. For the purposes of this definition,
“submitted” means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems, and
issue tracking systems that are managed by, or on behalf of, the Licensor for
the purpose of discussing and improving the Work, but excluding communication
that is conspicuously marked or otherwise designated in writing by the copyright
owner as “Not a Contribution.”
“Contributor” shall mean Licensor and any individual or Legal Entity on behalf
of whom a Contribution has been received by Licensor and subsequently
incorporated within the Work.
#### 2. Grant of Copyright License
Subject to the terms and conditions of this License, each Contributor hereby
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the Work and such
Derivative Works in Source or Object form.
#### 3. Grant of Patent License
Subject to the terms and conditions of this License, each Contributor hereby
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable (except as stated in this section) patent license to make, have
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
such license applies only to those patent claims licensable by such Contributor
that are necessarily infringed by their Contribution(s) alone or by combination
of their Contribution(s) with the Work to which such Contribution(s) was
submitted. If You institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
Contribution incorporated within the Work constitutes direct or contributory
patent infringement, then any patent licenses granted to You under this License
for that Work shall terminate as of the date such litigation is filed.
#### 4. Redistribution
You may reproduce and distribute copies of the Work or Derivative Works thereof
in any medium, with or without modifications, and in Source or Object form,
provided that You meet the following conditions:
* **(a)** You must give any other recipients of the Work or Derivative Works a copy of
this License; and
* **(b)** You must cause any modified files to carry prominent notices stating that You
changed the files; and
* **(c)** You must retain, in the Source form of any Derivative Works that You distribute,
all copyright, patent, trademark, and attribution notices from the Source form
of the Work, excluding those notices that do not pertain to any part of the
Derivative Works; and
* **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any
Derivative Works that You distribute must include a readable copy of the
attribution notices contained within such NOTICE file, excluding those notices
that do not pertain to any part of the Derivative Works, in at least one of the
following places: within a NOTICE text file distributed as part of the
Derivative Works; within the Source form or documentation, if provided along
with the Derivative Works; or, within a display generated by the Derivative
Works, if and wherever such third-party notices normally appear. The contents of
the NOTICE file are for informational purposes only and do not modify the
License. You may add Your own attribution notices within Derivative Works that
You distribute, alongside or as an addendum to the NOTICE text from the Work,
provided that such additional attribution notices cannot be construed as
modifying the License.
You may add Your own copyright statement to Your modifications and may provide
additional or different license terms and conditions for use, reproduction, or
distribution of Your modifications, or for any such Derivative Works as a whole,
provided Your use, reproduction, and distribution of the Work otherwise complies
with the conditions stated in this License.
#### 5. Submission of Contributions
Unless You explicitly state otherwise, any Contribution intentionally submitted
for inclusion in the Work by You to the Licensor shall be under the terms and
conditions of this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify the terms of
any separate license agreement you may have executed with Licensor regarding
such Contributions.
#### 6. Trademarks
This License does not grant permission to use the trade names, trademarks,
service marks, or product names of the Licensor, except as required for
reasonable and customary use in describing the origin of the Work and
reproducing the content of the NOTICE file.
#### 7. Disclaimer of Warranty
Unless required by applicable law or agreed to in writing, Licensor provides the
Work (and each Contributor provides its Contributions) on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
including, without limitation, any warranties or conditions of TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
solely responsible for determining the appropriateness of using or
redistributing the Work and assume any risks associated with Your exercise of
permissions under this License.
#### 8. Limitation of Liability
In no event and under no legal theory, whether in tort (including negligence),
contract, or otherwise, unless required by applicable law (such as deliberate
and grossly negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special, incidental,
or consequential damages of any character arising as a result of this License or
out of the use or inability to use the Work (including but not limited to
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
any and all other commercial damages or losses), even if such Contributor has
been advised of the possibility of such damages.
#### 9. Accepting Warranty or Additional Liability
While redistributing the Work or Derivative Works thereof, You may choose to
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
other liability obligations and/or rights consistent with this License. However,
in accepting such obligations, You may act only on Your own behalf and on Your
sole responsibility, not on behalf of any other Contributor, and only if You
agree to indemnify, defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason of your
accepting any such warranty or additional liability.
_END OF TERMS AND CONDITIONS_
### APPENDIX: How to apply the Apache License to your work
To apply the Apache License to your work, attach the following boilerplate
notice, with the fields enclosed by brackets `[]` replaced with your own
identifying information. (Don't include the brackets!) The text should be
enclosed in the appropriate comment syntax for the file format. We also
recommend that a file or class name and description of purpose be included on
the same “printed page” as the copyright notice for easier identification within
third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+7
View File
@@ -0,0 +1,7 @@
# BQ24295 USB charger
A driver for the TI `BQ24295` single-cell Li-ion USB battery charger: watchdog timer
control, BATFET (battery FET) enable/disable for shipping mode, USB power-good detection,
and status/version readback.
License: [Apache v2.0](LICENSE-Apache-2.0.md)
@@ -0,0 +1,5 @@
description: TI BQ24295 single-cell USB battery charger
include: ["i2c-device.yaml"]
compatible: "ti,bq24295"
+3
View File
@@ -0,0 +1,3 @@
dependencies:
- TactilityKernel
bindings: bindings
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <drivers/bq24295.h>
#ifdef __cplusplus
extern "C" {
#endif
DEFINE_DEVICETREE(bq24295, struct Bq24295Config)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/module.h>
#ifdef __cplusplus
extern "C" {
#endif
extern struct Module bq24295_module;
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <tactility/error.h>
struct Device;
#ifdef __cplusplus
extern "C" {
#endif
struct Bq24295Config {
/** Address on bus */
uint8_t address;
};
enum Bq24295WatchDogTimer {
BQ24295_WATCHDOG_DISABLED = 0b000000,
BQ24295_WATCHDOG_ENABLED_40S = 0b010000,
BQ24295_WATCHDOG_ENABLED_80S = 0b100000,
BQ24295_WATCHDOG_ENABLED_160S = 0b110000
};
error_t bq24295_get_watchdog_timer(struct Device* device, enum Bq24295WatchDogTimer* out);
error_t bq24295_set_watchdog_timer(struct Device* device, enum Bq24295WatchDogTimer in);
error_t bq24295_is_usb_power_connected(struct Device* device, bool* connected);
/** BATFET (battery FET): off disconnects the battery from the system (shipping mode). */
error_t bq24295_set_bat_fet_on(struct Device* device, bool on);
error_t bq24295_get_status(struct Device* device, uint8_t* value);
error_t bq24295_get_version(struct Device* device, uint8_t* value);
void bq24295_print_info(struct Device* device);
#ifdef __cplusplus
}
#endif
+270
View File
@@ -0,0 +1,270 @@
// SPDX-License-Identifier: Apache-2.0
#include <drivers/bq24295.h>
#include <bq24295_module.h>
#include <tactility/check.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/i2c_controller.h>
#include <tactility/drivers/power_supply.h>
#include <tactility/log.h>
#include <new>
#define TAG "BQ24295"
#define GET_CONFIG(device) (static_cast<const Bq24295Config*>((device)->config))
/** Reference:
* https://www.ti.com/lit/ds/symlink/bq24295.pdf
* https://gitlab.com/hamishcunningham/unphonelibrary/-/blob/main/unPhone.h?ref_type=heads
*/
static constexpr uint8_t REG_CHARGE_TERMINATION = 0x05U; // Datasheet page 35: Charge end/timer cntrl
static constexpr uint8_t REG_OPERATION_CONTROL = 0x07U; // Datasheet page 37: Misc operation control
static constexpr uint8_t REG_STATUS = 0x08U; // Datasheet page 38: System status
static constexpr uint8_t REG_VERSION = 0x0AU; // Datasheet page 38: Vendor/part/revision status
static constexpr TickType_t TIMEOUT = pdMS_TO_TICKS(50);
extern "C" {
extern Module bq24295_module;
static error_t read_charge_termination(Device* device, uint8_t* out) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
return i2c_controller_register8_get(parent, address, REG_CHARGE_TERMINATION, out, TIMEOUT);
}
error_t bq24295_get_watchdog_timer(Device* device, Bq24295WatchDogTimer* out) {
uint8_t value;
error_t err = read_charge_termination(device, &value);
if (err != ERROR_NONE) {
return err;
}
switch (value & 0b00110000) {
case BQ24295_WATCHDOG_DISABLED:
*out = BQ24295_WATCHDOG_DISABLED;
return ERROR_NONE;
case BQ24295_WATCHDOG_ENABLED_40S:
*out = BQ24295_WATCHDOG_ENABLED_40S;
return ERROR_NONE;
case BQ24295_WATCHDOG_ENABLED_80S:
*out = BQ24295_WATCHDOG_ENABLED_80S;
return ERROR_NONE;
case BQ24295_WATCHDOG_ENABLED_160S:
*out = BQ24295_WATCHDOG_ENABLED_160S;
return ERROR_NONE;
default:
return ERROR_RESOURCE;
}
}
error_t bq24295_set_watchdog_timer(Device* device, Bq24295WatchDogTimer in) {
uint8_t value;
error_t err = read_charge_termination(device, &value);
if (err != ERROR_NONE) {
return err;
}
uint8_t bits_to_set = 0b00110000 & static_cast<uint8_t>(in);
uint8_t value_cleared = value & 0b11001111;
uint8_t to_set = bits_to_set | value_cleared;
LOG_I(TAG, "WatchDogTimer: %02X -> %02X", value, to_set);
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
return i2c_controller_register8_set(parent, address, REG_CHARGE_TERMINATION, to_set, TIMEOUT);
}
error_t bq24295_set_bat_fet_on(Device* device, bool on) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
// bit 5 low means bat fet is on, bit 5 high means bat fet is off
if (on) {
return i2c_controller_register8_reset_bits(parent, address, REG_OPERATION_CONTROL, 1 << 5, TIMEOUT);
} else {
return i2c_controller_register8_set_bits(parent, address, REG_OPERATION_CONTROL, 1 << 5, TIMEOUT);
}
}
error_t bq24295_get_status(Device* device, uint8_t* value) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
return i2c_controller_register8_get(parent, address, REG_STATUS, value, TIMEOUT);
}
error_t bq24295_is_usb_power_connected(Device* device, bool* connected) {
uint8_t status;
error_t err = bq24295_get_status(device, &status);
if (err != ERROR_NONE) {
return err;
}
*connected = (status & (1 << 2)) != 0U;
return ERROR_NONE;
}
error_t bq24295_get_version(Device* device, uint8_t* value) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
return i2c_controller_register8_get(parent, address, REG_VERSION, value, TIMEOUT);
}
void bq24295_print_info(Device* device) {
uint8_t version, status, charge_termination;
if (bq24295_get_status(device, &status) == ERROR_NONE &&
bq24295_get_version(device, &version) == ERROR_NONE &&
read_charge_termination(device, &charge_termination) == ERROR_NONE) {
LOG_I(TAG, "Version %d, status %02X, charge termination %02X", version, status, charge_termination);
} else {
LOG_E(TAG, "Failed to retrieve version and/or status");
}
}
// region Power supply child device
static bool ps_supports_property(Device*, PowerSupplyProperty property) {
return property == POWER_SUPPLY_PROP_IS_CHARGING;
}
static error_t ps_get_property(Device* device, PowerSupplyProperty property, PowerSupplyPropertyValue* out_value) {
if (property != POWER_SUPPLY_PROP_IS_CHARGING) {
return ERROR_NOT_SUPPORTED;
}
// device_get_parent() here is the bq24295 device itself (this child's parent), not the I2C bus.
uint8_t status;
error_t error = bq24295_get_status(device_get_parent(device), &status);
if (error != ERROR_NONE) {
return error;
}
// Datasheet REG08 bits[5:4] (CHRG_STAT): 0 = not charging, 1 = pre-charge,
// 2 = fast charge, 3 = charge termination done. Only 1|2 count as actively charging.
uint8_t charge_status = (status >> 4) & 0x03;
out_value->int_value = (charge_status == 1 || charge_status == 2) ? 1 : 0;
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 BQ24295_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: bq24295_driver wires it up directly by pointer.
Driver bq24295_power_supply_driver = {
.name = "bq24295-power-supply",
.compatible = (const char*[]) { "bq24295-power-supply", nullptr },
.start_device = nullptr,
.stop_device = nullptr,
.api = &BQ24295_POWER_SUPPLY_API,
.device_type = &POWER_SUPPLY_TYPE,
.owner = &bq24295_module,
.internal = nullptr
};
struct Bq24295Internal {
Device* power_supply_device = nullptr;
};
static error_t create_power_supply_child(Device* parent, Device*& out_child) {
auto* child = new(std::nothrow) Device { .address = 0, .name = "bq24295-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, &bq24295_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;
}
// endregion
static error_t start(Device* device) {
auto* parent = device_get_parent(device);
check(device_get_type(parent) == &I2C_CONTROLLER_TYPE);
auto* internal = new(std::nothrow) Bq24295Internal();
if (internal == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
error_t error = create_power_supply_child(device, internal->power_supply_device);
if (error != ERROR_NONE) {
delete internal;
return error;
}
device_set_driver_data(device, internal);
return ERROR_NONE;
}
static error_t stop(Device* device) {
auto* internal = static_cast<Bq24295Internal*>(device_get_driver_data(device));
destroy_power_supply_child(internal->power_supply_device);
device_set_driver_data(device, nullptr);
delete internal;
return ERROR_NONE;
}
Driver bq24295_driver = {
.name = "bq24295",
.compatible = (const char*[]) { "ti,bq24295", nullptr },
.start_device = start,
.stop_device = stop,
.api = nullptr,
.device_type = nullptr,
.owner = &bq24295_module,
.internal = nullptr
};
}
+35
View File
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/check.h>
#include <tactility/driver.h>
#include <tactility/module.h>
extern "C" {
extern Driver bq24295_driver;
extern Driver bq24295_power_supply_driver;
static error_t start() {
/* We crash when construct fails, because if a single driver fails to construct,
* there is no guarantee that the previously constructed drivers can be destroyed */
check(driver_construct_add(&bq24295_driver) == ERROR_NONE);
check(driver_construct_add(&bq24295_power_supply_driver) == ERROR_NONE);
return ERROR_NONE;
}
static error_t stop() {
/* We crash when destruct fails, because if a single driver fails to destruct,
* there is no guarantee that the previously destroyed drivers can be recovered */
check(driver_remove_destruct(&bq24295_power_supply_driver) == ERROR_NONE);
check(driver_remove_destruct(&bq24295_driver) == ERROR_NONE);
return ERROR_NONE;
}
Module bq24295_module = {
.name = "bq24295",
.start = start,
.stop = stop,
.symbols = nullptr,
.internal = nullptr
};
}