Migrate devices, create drivers, other improvements & fixes (#574)

Migrated devices to kernel drivers:

- guition-jc3248w535c
- m5stack-core2
- m5stack-cores3
- m5stack-papers3

New drivers:

- axp192-module
- axp2101-module

Fixes:

- Fix SD card LDO for P4 devices
- Updated PowerOff app to work with kernel displays
- Fix for `lvgl_try_lock()` timing
- Fix for touch events with slow updating displays

Improvements:

- `GuiService` now keeps trying to lock to prevent silent failures caused by drivers.
- `GuiService` now uses lvgl-module calls for locking/unlocking
- display driver now has capability `DISPLAY_CAPABILITY_SLOW_REFRESH`
This commit is contained in:
Ken Van Hoeylandt
2026-07-19 00:39:26 +02:00
committed by GitHub
parent 5f54f7ca3d
commit f9453d8956
119 changed files with 3327 additions and 3994 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(axp192-module
SRCS ${SOURCE_FILES}
INCLUDE_DIRS include/
REQUIRES TactilityKernel
)
+195
View File
@@ -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.
+13
View File
@@ -0,0 +1,13 @@
# AXP192 power management IC
A driver for the X-Powers `AXP192` PMIC: DCDC1-3/LDO2-3/EXTEN rail enable and voltage
control, battery voltage/current/power readback, charge enable/status, and system power off.
Registers as a `power-supply` child device (voltage, current, is-charging, charge control,
power off) alongside its own public API for full rail control.
Also includes `axp192-backlight`: a `BACKLIGHT_TYPE` driver for a backlight wired to one of
the AXP192's rails, declared as a devicetree child node of the `axp192` device it dims. Its
`rail`, `min-millivolt` and `max-millivolt` properties select the channel and map the 0-255
brightness level onto that rail's voltage range; brightness level 0 disables the rail outright.
License: [Apache v2.0](LICENSE-Apache-2.0.md)
@@ -0,0 +1,24 @@
description: >
Backlight driven by a switchable/adjustable AXP192 power rail. Must be declared as a
child node of the axp192 device it dims. Maps the 0-255 brightness level onto the
rail's [min-millivolt,max-millivolt] voltage range; level 0 disables the rail outright.
compatible: "axp192-backlight"
properties:
rail:
type: int
required: true
description: The Axp192Rail powering the backlight (e.g. AXP192_RAIL_LDO2)
min-millivolt:
type: int
default: 0
description: Rail voltage at brightness level 0 (the rail is disabled rather than actually set to this value)
max-millivolt:
type: int
required: true
description: Rail voltage at the maximum brightness level (255)
brightness-default:
type: int
default: 255
description: Default brightness level, applied by set_brightness_default()
@@ -0,0 +1,5 @@
description: X-Powers AXP192 power management IC
include: ["i2c-device.yaml"]
compatible: "x-powers,axp192"
+3
View File
@@ -0,0 +1,3 @@
dependencies:
- TactilityKernel
bindings: bindings
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/module.h>
#ifdef __cplusplus
extern "C" {
#endif
extern struct Module axp192_module;
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <drivers/axp192.h>
#ifdef __cplusplus
extern "C" {
#endif
DEFINE_DEVICETREE(axp192, struct Axp192Config)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <drivers/axp192_backlight.h>
#ifdef __cplusplus
extern "C" {
#endif
DEFINE_DEVICETREE(axp192_backlight, struct Axp192BacklightConfig)
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,81 @@
// 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 Axp192Config {
/** Address on bus */
uint8_t address;
};
/** Switchable/adjustable power rails of the AXP192. */
enum Axp192Rail {
AXP192_RAIL_DCDC1,
AXP192_RAIL_DCDC2,
AXP192_RAIL_DCDC3,
AXP192_RAIL_LDO2,
AXP192_RAIL_LDO3,
/** EXTEN output switch; does not support voltage control. */
AXP192_RAIL_EXTEN,
};
/**
* @brief Checks whether a power rail is currently enabled.
*/
error_t axp192_is_rail_enabled(struct Device* device, enum Axp192Rail rail, bool* enabled);
/**
* @brief Enables or disables a power rail.
*/
error_t axp192_set_rail_enabled(struct Device* device, enum Axp192Rail rail, bool enabled);
/**
* @brief Sets the output voltage of a power rail.
* @retval ERROR_NOT_SUPPORTED for AXP192_RAIL_EXTEN, which has no voltage control
* @retval ERROR_INVALID_ARGUMENT when millivolts is outside the rail's supported range
* (DCDC1/DCDC3: 700-3500mV, DCDC2: 700-2275mV, LDO2/LDO3: 1800-3300mV)
*/
error_t axp192_set_rail_voltage(struct Device* device, enum Axp192Rail rail, uint16_t millivolts);
/** Battery voltage in millivolts. */
error_t axp192_get_battery_voltage(struct Device* device, uint16_t* millivolts);
/** Battery charge current in milliamps (0 when not charging). */
error_t axp192_get_battery_charge_current(struct Device* device, uint16_t* milliamps);
/** Battery discharge current in milliamps (0 when not discharging). */
error_t axp192_get_battery_discharge_current(struct Device* device, uint16_t* milliamps);
/** Battery power in microwatts. */
error_t axp192_get_battery_power(struct Device* device, uint32_t* microwatts);
/** Whether the battery is currently charging. */
error_t axp192_is_charging(struct Device* device, bool* charging);
/** Whether the charger is allowed to charge the battery. */
error_t axp192_is_charge_enabled(struct Device* device, bool* enabled);
/** Enables or disables battery charging. */
error_t axp192_set_charge_enabled(struct Device* device, bool enabled);
/** Powers off the system (does not return on success). */
error_t axp192_power_off(struct Device* device);
/** Configures GPIO1 as the PWM1 output (rather than GPIO/ADC input/output). */
error_t axp192_set_gpio1_pwm1_output(struct Device* device);
/** Sets the PWM1 duty cycle (0 = always low, 255 = always high). */
error_t axp192_set_pwm1_duty_cycle(struct Device* device, uint8_t duty);
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <stdint.h>
#include <drivers/axp192.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Devicetree configuration for a backlight driven by a switchable/adjustable AXP192 power rail.
*/
struct Axp192BacklightConfig {
/** The AXP192 rail powering the backlight */
enum Axp192Rail rail;
/** Rail voltage at brightness level 0. Brightness level 0 always disables the rail outright,
* rather than actually driving it to this voltage - see set_brightness() in BacklightApi. */
uint16_t min_millivolt;
/** Rail voltage at the maximum brightness level (255) */
uint16_t max_millivolt;
/** Default brightness level, applied by set_brightness_default() */
uint8_t brightness_default;
};
#ifdef __cplusplus
}
#endif
+445
View File
@@ -0,0 +1,445 @@
// SPDX-License-Identifier: Apache-2.0
#include <drivers/axp192.h>
#include <axp192_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 GET_CONFIG(device) (static_cast<const Axp192Config*>((device)->config))
/** Reference: https://github.com/tuupola/axp192 (register map and ADC/voltage formulas) */
static constexpr uint8_t REG_MODE_CHGSTATUS = 0x01U; // bit6: battery is charging
static constexpr uint8_t REG_EXTEN_DCDC2_CONTROL = 0x10U; // bit2: EXTEN, bit0: DCDC2
static constexpr uint8_t REG_DCDC13_LDO23_CONTROL = 0x12U; // bit3: LDO3, bit2: LDO2, bit1: DCDC3, bit0: DCDC1
static constexpr uint8_t REG_DCDC2_VOLTAGE = 0x23U;
static constexpr uint8_t REG_DCDC1_VOLTAGE = 0x26U;
static constexpr uint8_t REG_DCDC3_VOLTAGE = 0x27U;
static constexpr uint8_t REG_LDO23_VOLTAGE = 0x28U; // bits7-4: LDO2, bits3-0: LDO3
static constexpr uint8_t REG_SHUTDOWN_BATTERY_CHGLED_CONTROL = 0x32U; // bit7: power off
static constexpr uint8_t REG_CHARGE_CONTROL_1 = 0x33U; // bit7: charging enabled
static constexpr uint8_t REG_BATTERY_POWER = 0x70U; // 3 bytes, 0.55uW/LSB
static constexpr uint8_t REG_BATTERY_VOLTAGE = 0x78U; // 2 bytes, 1.1mV/LSB
static constexpr uint8_t REG_CHARGE_CURRENT = 0x7AU; // 2 bytes, 0.5mA/LSB
static constexpr uint8_t REG_DISCHARGE_CURRENT = 0x7CU; // 2 bytes, 0.5mA/LSB
static constexpr uint8_t REG_GPIO1_CONTROL = 0x92U; // function select (0x02 = PWM1 output)
static constexpr uint8_t REG_PWM1_DUTY_CYCLE_2 = 0x9AU; // PWM1 duty cycle, low byte
static constexpr uint8_t GPIO1_FUNCTION_PWM1 = 0x02U;
static constexpr uint8_t BIT_CHARGING = 1U << 6U;
static constexpr uint8_t BIT_CHARGE_ENABLED = 1U << 7U;
static constexpr uint8_t BIT_POWER_OFF = 1U << 7U;
static constexpr TickType_t TIMEOUT = pdMS_TO_TICKS(50);
extern "C" {
extern Module axp192_module;
// region Register helpers
/** Reads a 2-byte ADC register in AXP192's packed 12-bit format: raw = (high << 4) + low. */
static error_t read_adc_raw(Device* device, uint8_t reg, uint16_t* raw) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
uint8_t buffer[2];
error_t err = i2c_controller_read_register(parent, address, reg, buffer, sizeof(buffer), TIMEOUT);
if (err != ERROR_NONE) {
return err;
}
*raw = static_cast<uint16_t>((buffer[0] << 4U) + (buffer[1] & 0x0FU));
return ERROR_NONE;
}
static error_t get_rail_enable_bit(Axp192Rail rail, uint8_t* reg, uint8_t* bit) {
switch (rail) {
case AXP192_RAIL_DCDC1:
*reg = REG_DCDC13_LDO23_CONTROL;
*bit = 1U << 0U;
return ERROR_NONE;
case AXP192_RAIL_DCDC2:
*reg = REG_EXTEN_DCDC2_CONTROL;
*bit = 1U << 0U;
return ERROR_NONE;
case AXP192_RAIL_DCDC3:
*reg = REG_DCDC13_LDO23_CONTROL;
*bit = 1U << 1U;
return ERROR_NONE;
case AXP192_RAIL_LDO2:
*reg = REG_DCDC13_LDO23_CONTROL;
*bit = 1U << 2U;
return ERROR_NONE;
case AXP192_RAIL_LDO3:
*reg = REG_DCDC13_LDO23_CONTROL;
*bit = 1U << 3U;
return ERROR_NONE;
case AXP192_RAIL_EXTEN:
*reg = REG_EXTEN_DCDC2_CONTROL;
*bit = 1U << 2U;
return ERROR_NONE;
}
return ERROR_INVALID_ARGUMENT;
}
// endregion
error_t axp192_is_rail_enabled(Device* device, Axp192Rail rail, bool* enabled) {
uint8_t reg, bit;
error_t err = get_rail_enable_bit(rail, &reg, &bit);
if (err != ERROR_NONE) {
return err;
}
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
uint8_t value;
err = i2c_controller_register8_get(parent, address, reg, &value, TIMEOUT);
if (err != ERROR_NONE) {
return err;
}
*enabled = (value & bit) != 0U;
return ERROR_NONE;
}
error_t axp192_set_rail_enabled(Device* device, Axp192Rail rail, bool enabled) {
uint8_t reg, bit;
error_t err = get_rail_enable_bit(rail, &reg, &bit);
if (err != ERROR_NONE) {
return err;
}
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
if (enabled) {
return i2c_controller_register8_set_bits(parent, address, reg, bit, TIMEOUT);
} else {
return i2c_controller_register8_reset_bits(parent, address, reg, bit, TIMEOUT);
}
}
error_t axp192_set_rail_voltage(Device* device, Axp192Rail rail, uint16_t millivolts) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
switch (rail) {
case AXP192_RAIL_DCDC1:
case AXP192_RAIL_DCDC3: {
if (millivolts < 700U || millivolts > 3500U) {
return ERROR_INVALID_ARGUMENT;
}
uint8_t reg = (rail == AXP192_RAIL_DCDC1) ? REG_DCDC1_VOLTAGE : REG_DCDC3_VOLTAGE;
uint8_t value = static_cast<uint8_t>((millivolts - 700U) / 25U);
return i2c_controller_register8_set(parent, address, reg, value, TIMEOUT);
}
case AXP192_RAIL_DCDC2: {
if (millivolts < 700U || millivolts > 2275U) {
return ERROR_INVALID_ARGUMENT;
}
uint8_t value = static_cast<uint8_t>((millivolts - 700U) / 25U);
return i2c_controller_register8_set(parent, address, REG_DCDC2_VOLTAGE, value, TIMEOUT);
}
case AXP192_RAIL_LDO2:
case AXP192_RAIL_LDO3: {
if (millivolts < 1800U || millivolts > 3300U) {
return ERROR_INVALID_ARGUMENT;
}
uint8_t step = static_cast<uint8_t>((millivolts - 1800U) / 100U);
uint8_t value;
error_t err = i2c_controller_register8_get(parent, address, REG_LDO23_VOLTAGE, &value, TIMEOUT);
if (err != ERROR_NONE) {
return err;
}
if (rail == AXP192_RAIL_LDO2) {
value = static_cast<uint8_t>((value & 0x0FU) | (step << 4U));
} else {
value = static_cast<uint8_t>((value & 0xF0U) | step);
}
return i2c_controller_register8_set(parent, address, REG_LDO23_VOLTAGE, value, TIMEOUT);
}
case AXP192_RAIL_EXTEN:
return ERROR_NOT_SUPPORTED;
}
return ERROR_INVALID_ARGUMENT;
}
error_t axp192_get_battery_voltage(Device* device, uint16_t* millivolts) {
uint16_t raw;
error_t err = read_adc_raw(device, REG_BATTERY_VOLTAGE, &raw);
if (err != ERROR_NONE) {
return err;
}
*millivolts = static_cast<uint16_t>((raw * 11U) / 10U); // 1.1mV/LSB
return ERROR_NONE;
}
error_t axp192_get_battery_charge_current(Device* device, uint16_t* milliamps) {
uint16_t raw;
error_t err = read_adc_raw(device, REG_CHARGE_CURRENT, &raw);
if (err != ERROR_NONE) {
return err;
}
*milliamps = raw / 2U; // 0.5mA/LSB
return ERROR_NONE;
}
error_t axp192_get_battery_discharge_current(Device* device, uint16_t* milliamps) {
uint16_t raw;
error_t err = read_adc_raw(device, REG_DISCHARGE_CURRENT, &raw);
if (err != ERROR_NONE) {
return err;
}
*milliamps = raw / 2U; // 0.5mA/LSB
return ERROR_NONE;
}
error_t axp192_get_battery_power(Device* device, uint32_t* microwatts) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
uint8_t buffer[3];
error_t err = i2c_controller_read_register(parent, address, REG_BATTERY_POWER, buffer, sizeof(buffer), TIMEOUT);
if (err != ERROR_NONE) {
return err;
}
uint32_t raw = (static_cast<uint32_t>(buffer[0]) << 16U) | (static_cast<uint32_t>(buffer[1]) << 8U) | buffer[2];
*microwatts = (raw * 11U) / 20U; // 1.1mV * 0.5mA = 0.55uW/LSB
return ERROR_NONE;
}
error_t axp192_is_charging(Device* device, bool* charging) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
uint8_t value;
error_t err = i2c_controller_register8_get(parent, address, REG_MODE_CHGSTATUS, &value, TIMEOUT);
if (err != ERROR_NONE) {
return err;
}
*charging = (value & BIT_CHARGING) != 0U;
return ERROR_NONE;
}
error_t axp192_is_charge_enabled(Device* device, bool* enabled) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
uint8_t value;
error_t err = i2c_controller_register8_get(parent, address, REG_CHARGE_CONTROL_1, &value, TIMEOUT);
if (err != ERROR_NONE) {
return err;
}
*enabled = (value & BIT_CHARGE_ENABLED) != 0U;
return ERROR_NONE;
}
error_t axp192_set_charge_enabled(Device* device, bool enabled) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
if (enabled) {
return i2c_controller_register8_set_bits(parent, address, REG_CHARGE_CONTROL_1, BIT_CHARGE_ENABLED, TIMEOUT);
} else {
return i2c_controller_register8_reset_bits(parent, address, REG_CHARGE_CONTROL_1, BIT_CHARGE_ENABLED, TIMEOUT);
}
}
error_t axp192_power_off(Device* device) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
return i2c_controller_register8_set_bits(parent, address, REG_SHUTDOWN_BATTERY_CHGLED_CONTROL, BIT_POWER_OFF, TIMEOUT);
}
error_t axp192_set_gpio1_pwm1_output(Device* device) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
return i2c_controller_register8_set(parent, address, REG_GPIO1_CONTROL, GPIO1_FUNCTION_PWM1, TIMEOUT);
}
error_t axp192_set_pwm1_duty_cycle(Device* device, uint8_t duty) {
auto* parent = device_get_parent(device);
auto address = GET_CONFIG(device)->address;
return i2c_controller_register8_set(parent, address, REG_PWM1_DUTY_CYCLE_2, duty, TIMEOUT);
}
// region Power supply child device
static bool ps_supports_property(Device*, PowerSupplyProperty property) {
return property == POWER_SUPPLY_PROP_IS_CHARGING ||
property == POWER_SUPPLY_PROP_VOLTAGE ||
property == POWER_SUPPLY_PROP_CURRENT;
}
static error_t ps_get_property(Device* device, PowerSupplyProperty property, PowerSupplyPropertyValue* out_value) {
// device_get_parent() here is the axp192 device itself (this child's parent), not the I2C bus.
auto* axp192_device = device_get_parent(device);
switch (property) {
case POWER_SUPPLY_PROP_IS_CHARGING: {
bool charging;
error_t err = axp192_is_charging(axp192_device, &charging);
if (err != ERROR_NONE) {
return err;
}
out_value->int_value = charging ? 1 : 0;
return ERROR_NONE;
}
case POWER_SUPPLY_PROP_VOLTAGE: {
uint16_t millivolts;
error_t err = axp192_get_battery_voltage(axp192_device, &millivolts);
if (err != ERROR_NONE) {
return err;
}
out_value->int_value = millivolts;
return ERROR_NONE;
}
case POWER_SUPPLY_PROP_CURRENT: {
uint16_t charge_current, discharge_current;
error_t err = axp192_get_battery_charge_current(axp192_device, &charge_current);
if (err != ERROR_NONE) {
return err;
}
err = axp192_get_battery_discharge_current(axp192_device, &discharge_current);
if (err != ERROR_NONE) {
return err;
}
out_value->int_value = (charge_current > 0U) ? charge_current : -static_cast<int>(discharge_current);
return ERROR_NONE;
}
default:
return ERROR_NOT_SUPPORTED;
}
}
static bool ps_supports_charge_control(Device*) { return true; }
static bool ps_is_allowed_to_charge(Device* device) {
bool enabled = false;
axp192_is_charge_enabled(device_get_parent(device), &enabled);
return enabled;
}
static error_t ps_set_allowed_to_charge(Device* device, bool allowed) {
return axp192_set_charge_enabled(device_get_parent(device), allowed);
}
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 true; }
static error_t ps_power_off(Device* device) { return axp192_power_off(device_get_parent(device)); }
static constexpr PowerSupplyApi AXP192_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: axp192_driver wires it up directly by pointer.
Driver axp192_power_supply_driver = {
.name = "axp192-power-supply",
.compatible = (const char*[]) { "axp192-power-supply", nullptr },
.start_device = nullptr,
.stop_device = nullptr,
.api = &AXP192_POWER_SUPPLY_API,
.device_type = &POWER_SUPPLY_TYPE,
.owner = &axp192_module,
.internal = nullptr
};
struct Axp192Internal {
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 = "axp192-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, &axp192_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) Axp192Internal();
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<Axp192Internal*>(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 axp192_driver = {
.name = "axp192",
.compatible = (const char*[]) { "x-powers,axp192", nullptr },
.start_device = start,
.stop_device = stop,
.api = nullptr,
.device_type = nullptr,
.owner = &axp192_module,
.internal = nullptr
};
}
@@ -0,0 +1,132 @@
// SPDX-License-Identifier: Apache-2.0
#include <drivers/axp192_backlight.h>
#include <axp192_module.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/backlight.h>
#include <tactility/log.h>
#include <tactility/module.h>
#include <new>
constexpr auto* TAG = "Axp192Backlight";
#define GET_CONFIG(device) (static_cast<const Axp192BacklightConfig*>((device)->config))
#define GET_INTERNAL(device) (static_cast<Axp192BacklightInternal*>(device_get_driver_data(device)))
extern "C" {
struct Axp192BacklightInternal {
uint8_t brightness;
};
// region BacklightApi
static error_t apply_brightness(Device* device, uint8_t brightness) {
const auto* config = GET_CONFIG(device);
auto* axp192 = device_get_parent(device);
if (brightness == 0) {
return axp192_set_rail_enabled(axp192, config->rail, false);
}
uint16_t millivolt = static_cast<uint16_t>(
config->min_millivolt +
(static_cast<uint32_t>(brightness) * (config->max_millivolt - config->min_millivolt)) / 255U
);
error_t error = axp192_set_rail_voltage(axp192, config->rail, millivolt);
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to set rail voltage");
return error;
}
return axp192_set_rail_enabled(axp192, config->rail, true);
}
static error_t axp192_backlight_set_brightness(Device* device, uint8_t brightness) {
error_t error = apply_brightness(device, brightness);
if (error != ERROR_NONE) {
return error;
}
GET_INTERNAL(device)->brightness = brightness;
return ERROR_NONE;
}
static error_t axp192_backlight_set_brightness_default(Device* device) {
return axp192_backlight_set_brightness(device, GET_CONFIG(device)->brightness_default);
}
static error_t axp192_backlight_get_brightness(Device* device, uint8_t* out_brightness) {
*out_brightness = GET_INTERNAL(device)->brightness;
return ERROR_NONE;
}
static uint8_t axp192_backlight_get_min_brightness(Device*) {
return 0;
}
static uint8_t axp192_backlight_get_max_brightness(Device*) {
return 255;
}
// endregion
static constexpr BacklightApi AXP192_BACKLIGHT_API = {
.set_brightness = axp192_backlight_set_brightness,
.set_brightness_default = axp192_backlight_set_brightness_default,
.get_brightness = axp192_backlight_get_brightness,
.get_min_brightness = axp192_backlight_get_min_brightness,
.get_max_brightness = axp192_backlight_get_max_brightness,
};
// region Driver lifecycle
static error_t start(Device* device) {
const auto* config = GET_CONFIG(device);
if (config->max_millivolt <= config->min_millivolt) {
return ERROR_INVALID_ARGUMENT;
}
auto* internal = new(std::nothrow) Axp192BacklightInternal { .brightness = 0 };
if (internal == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
device_set_driver_data(device, internal);
error_t error = axp192_backlight_set_brightness_default(device);
if (error != ERROR_NONE) {
device_set_driver_data(device, nullptr);
delete internal;
return error;
}
return ERROR_NONE;
}
static error_t stop(Device* device) {
axp192_backlight_set_brightness(device, 0); // Allowed to fail, we don't care about the result
auto* internal = GET_INTERNAL(device);
device_set_driver_data(device, nullptr);
delete internal;
return ERROR_NONE;
}
// endregion
Driver axp192_backlight_driver = {
.name = "axp192_backlight",
.compatible = (const char*[]) { "axp192-backlight", nullptr },
.start_device = start,
.stop_device = stop,
.api = &AXP192_BACKLIGHT_API,
.device_type = &BACKLIGHT_TYPE,
.owner = &axp192_module,
.internal = nullptr
};
}
+56
View File
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: Apache-2.0
#include <drivers/axp192.h>
#include <tactility/check.h>
#include <tactility/driver.h>
#include <tactility/module.h>
extern "C" {
extern Driver axp192_driver;
extern Driver axp192_power_supply_driver;
extern Driver axp192_backlight_driver;
const struct ModuleSymbol axp192_module_symbols[] = {
DEFINE_MODULE_SYMBOL(axp192_is_rail_enabled),
DEFINE_MODULE_SYMBOL(axp192_set_rail_enabled),
DEFINE_MODULE_SYMBOL(axp192_set_rail_voltage),
DEFINE_MODULE_SYMBOL(axp192_get_battery_voltage),
DEFINE_MODULE_SYMBOL(axp192_get_battery_charge_current),
DEFINE_MODULE_SYMBOL(axp192_get_battery_discharge_current),
DEFINE_MODULE_SYMBOL(axp192_get_battery_power),
DEFINE_MODULE_SYMBOL(axp192_is_charging),
DEFINE_MODULE_SYMBOL(axp192_is_charge_enabled),
DEFINE_MODULE_SYMBOL(axp192_set_charge_enabled),
DEFINE_MODULE_SYMBOL(axp192_power_off),
DEFINE_MODULE_SYMBOL(axp192_set_gpio1_pwm1_output),
DEFINE_MODULE_SYMBOL(axp192_set_pwm1_duty_cycle),
MODULE_SYMBOL_TERMINATOR
};
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(&axp192_driver) == ERROR_NONE);
check(driver_construct_add(&axp192_power_supply_driver) == ERROR_NONE);
check(driver_construct_add(&axp192_backlight_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(&axp192_backlight_driver) == ERROR_NONE);
check(driver_remove_destruct(&axp192_power_supply_driver) == ERROR_NONE);
check(driver_remove_destruct(&axp192_driver) == ERROR_NONE);
return ERROR_NONE;
}
Module axp192_module = {
.name = "axp192",
.start = start,
.stop = stop,
.symbols = axp192_module_symbols,
.internal = nullptr
};
}