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:
committed by
GitHub
parent
5f54f7ca3d
commit
f9453d8956
@@ -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(axp2101-module
|
||||
SRCS ${SOURCE_FILES}
|
||||
INCLUDE_DIRS include/
|
||||
REQUIRES TactilityKernel
|
||||
)
|
||||
@@ -0,0 +1,195 @@
|
||||
Apache License
|
||||
==============
|
||||
|
||||
_Version 2.0, January 2004_
|
||||
_<<http://www.apache.org/licenses/>>_
|
||||
|
||||
### 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.
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
# AXP2101 power management IC
|
||||
|
||||
A driver for the X-Powers `AXP2101` PMIC: DCDC1-5 and ALDO1-4/BLDO1-2/CPUSLDO/DLDO1-2
|
||||
enable and voltage control, battery/VBUS voltage readback, charge enable/status, and
|
||||
system power off. Registers as a `power-supply` child device (voltage, is-charging,
|
||||
charge control, power off) alongside its own public API for full rail control.
|
||||
|
||||
Also includes `axp2101-backlight`: a `BACKLIGHT_TYPE` driver for a backlight wired to one of
|
||||
the AXP2101's LDOs, declared as a devicetree child node of the `axp2101` device it dims. Its
|
||||
`ldo`, `min-millivolt` and `max-millivolt` properties select the channel and map the 0-255
|
||||
brightness level onto that LDO's voltage range; brightness level 0 disables the LDO outright.
|
||||
|
||||
License: [Apache v2.0](LICENSE-Apache-2.0.md)
|
||||
@@ -0,0 +1,24 @@
|
||||
description: >
|
||||
Backlight driven by a switchable/adjustable AXP2101 LDO. Must be declared as a
|
||||
child node of the axp2101 device it dims. Maps the 0-255 brightness level onto
|
||||
the LDO's [min-millivolt,max-millivolt] voltage range; level 0 disables the LDO outright.
|
||||
|
||||
compatible: "axp2101-backlight"
|
||||
|
||||
properties:
|
||||
ldo:
|
||||
type: int
|
||||
required: true
|
||||
description: The Axp2101Ldo powering the backlight (e.g. AXP2101_DLDO1)
|
||||
min-millivolt:
|
||||
type: int
|
||||
default: 0
|
||||
description: LDO voltage at brightness level 0 (the LDO is disabled rather than actually set to this value)
|
||||
max-millivolt:
|
||||
type: int
|
||||
required: true
|
||||
description: LDO 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 AXP2101 power management IC
|
||||
|
||||
include: ["i2c-device.yaml"]
|
||||
|
||||
compatible: "x-powers,axp2101"
|
||||
@@ -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 axp2101_module;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/bindings/bindings.h>
|
||||
#include <drivers/axp2101.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
DEFINE_DEVICETREE(axp2101, struct Axp2101Config)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/bindings/bindings.h>
|
||||
#include <drivers/axp2101_backlight.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
DEFINE_DEVICETREE(axp2101_backlight, struct Axp2101BacklightConfig)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,96 @@
|
||||
// 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 Axp2101Config {
|
||||
/** Address on bus */
|
||||
uint8_t address;
|
||||
};
|
||||
|
||||
/** Switchable/adjustable DCDC (buck) converters of the AXP2101. */
|
||||
enum Axp2101Dcdc {
|
||||
AXP2101_DCDC1,
|
||||
AXP2101_DCDC2,
|
||||
AXP2101_DCDC3,
|
||||
AXP2101_DCDC4,
|
||||
AXP2101_DCDC5,
|
||||
};
|
||||
|
||||
/** Switchable/adjustable LDO regulators of the AXP2101. */
|
||||
enum Axp2101Ldo {
|
||||
AXP2101_ALDO1,
|
||||
AXP2101_ALDO2,
|
||||
AXP2101_ALDO3,
|
||||
AXP2101_ALDO4,
|
||||
AXP2101_BLDO1,
|
||||
AXP2101_BLDO2,
|
||||
AXP2101_CPUSLDO,
|
||||
AXP2101_DLDO1,
|
||||
AXP2101_DLDO2,
|
||||
};
|
||||
|
||||
/** Checks whether a DCDC converter is currently enabled. */
|
||||
error_t axp2101_is_dcdc_enabled(struct Device* device, enum Axp2101Dcdc dcdc, bool* enabled);
|
||||
|
||||
/** Enables or disables a DCDC converter. */
|
||||
error_t axp2101_set_dcdc_enabled(struct Device* device, enum Axp2101Dcdc dcdc, bool enabled);
|
||||
|
||||
/**
|
||||
* @brief Sets the output voltage of a DCDC converter.
|
||||
* @retval ERROR_INVALID_ARGUMENT when millivolts is outside the converter's supported range/step
|
||||
* (DCDC1: 1500-3400mV/100mV; DCDC2: 500-1200mV/10mV or 1220-1540mV/20mV;
|
||||
* DCDC3: 500-1200mV/10mV, 1220-1540mV/20mV or 1600-3400mV/100mV;
|
||||
* DCDC4: 500-1200mV/10mV or 1220-1840mV/20mV; DCDC5: 1200mV, or 1400-3700mV/100mV)
|
||||
*/
|
||||
error_t axp2101_set_dcdc_voltage(struct Device* device, enum Axp2101Dcdc dcdc, uint16_t millivolts);
|
||||
|
||||
/** Checks whether an LDO regulator is currently enabled. */
|
||||
error_t axp2101_is_ldo_enabled(struct Device* device, enum Axp2101Ldo ldo, bool* enabled);
|
||||
|
||||
/** Enables or disables an LDO regulator. */
|
||||
error_t axp2101_set_ldo_enabled(struct Device* device, enum Axp2101Ldo ldo, bool enabled);
|
||||
|
||||
/**
|
||||
* @brief Sets the output voltage of an LDO regulator.
|
||||
* @retval ERROR_INVALID_ARGUMENT when millivolts is outside the regulator's supported range/step
|
||||
* (ALDO1-4/BLDO1-2: 500-3500mV/100mV; CPUSLDO: 500-1400mV/50mV; DLDO1-2: 500-3400mV/100mV)
|
||||
*/
|
||||
error_t axp2101_set_ldo_voltage(struct Device* device, enum Axp2101Ldo ldo, uint16_t millivolts);
|
||||
|
||||
/** Battery voltage in millivolts (0 when no battery is connected). */
|
||||
error_t axp2101_get_battery_voltage(struct Device* device, uint16_t* millivolts);
|
||||
|
||||
/** Whether a battery is currently detected. */
|
||||
error_t axp2101_is_battery_connected(struct Device* device, bool* connected);
|
||||
|
||||
/** Whether VBUS (USB power) is currently present and usable. */
|
||||
error_t axp2101_is_vbus_present(struct Device* device, bool* present);
|
||||
|
||||
/** VBUS voltage in millivolts (0 when VBUS is not present). */
|
||||
error_t axp2101_get_vbus_voltage(struct Device* device, uint16_t* millivolts);
|
||||
|
||||
/** Whether the battery is currently charging. */
|
||||
error_t axp2101_is_charging(struct Device* device, bool* charging);
|
||||
|
||||
/** Whether the charger is allowed to charge the battery. */
|
||||
error_t axp2101_is_charge_enabled(struct Device* device, bool* enabled);
|
||||
|
||||
/** Enables or disables battery charging. */
|
||||
error_t axp2101_set_charge_enabled(struct Device* device, bool enabled);
|
||||
|
||||
/** Powers off the system (does not return on success). */
|
||||
error_t axp2101_power_off(struct Device* device);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <drivers/axp2101.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Devicetree configuration for a backlight driven by a switchable/adjustable AXP2101 LDO.
|
||||
*/
|
||||
struct Axp2101BacklightConfig {
|
||||
/** The AXP2101 LDO powering the backlight */
|
||||
enum Axp2101Ldo ldo;
|
||||
/** LDO voltage at brightness level 0. Brightness level 0 always disables the LDO outright,
|
||||
* rather than actually driving it to this voltage - see set_brightness() in BacklightApi. */
|
||||
uint16_t min_millivolt;
|
||||
/** LDO 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
|
||||
@@ -0,0 +1,555 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <drivers/axp2101.h>
|
||||
#include <axp2101_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 "AXP2101"
|
||||
#define GET_CONFIG(device) (static_cast<const Axp2101Config*>((device)->config))
|
||||
|
||||
/** Reference: https://github.com/lewisxhe/XPowersLib (AXP2101 register map and voltage-encoding formulas) */
|
||||
static constexpr uint8_t REG_STATUS1 = 0x00U; // bit5: VBUS good
|
||||
static constexpr uint8_t REG_STATUS2 = 0x01U; // bits[6:5]: charge status (1=charging), bit3: battery connected(?)
|
||||
static constexpr uint8_t REG_COMMON_CONFIG = 0x10U; // bit0: shutdown
|
||||
static constexpr uint8_t REG_CHARGE_GAUGE_WDT_CTRL = 0x18U; // bit1: charge enable
|
||||
static constexpr uint8_t REG_ADC_DATA_RESULT0 = 0x34U; // battery voltage, high 5 bits
|
||||
static constexpr uint8_t REG_ADC_DATA_RESULT1 = 0x35U; // battery voltage, low 8 bits
|
||||
static constexpr uint8_t REG_ADC_DATA_RESULT4 = 0x38U; // VBUS voltage, high 6 bits
|
||||
static constexpr uint8_t REG_ADC_DATA_RESULT5 = 0x39U; // VBUS voltage, low 8 bits
|
||||
static constexpr uint8_t REG_DC_ONOFF_DVM_CTRL = 0x80U; // bits0-4: DCDC1-5 enable
|
||||
static constexpr uint8_t REG_DC_VOL0_CTRL = 0x82U; // DCDC1 voltage; DCDC2-5 follow at +1..+4
|
||||
static constexpr uint8_t REG_LDO_ONOFF_CTRL0 = 0x90U; // bits0-7: ALDO1-4, BLDO1-2, CPUSLDO, DLDO1 enable
|
||||
static constexpr uint8_t REG_LDO_ONOFF_CTRL1 = 0x91U; // bit0: DLDO2 enable
|
||||
static constexpr uint8_t REG_LDO_VOL0_CTRL = 0x92U; // ALDO1 voltage; remaining LDOs follow at +1..+8
|
||||
static constexpr uint8_t REG_ADC_CHANNEL_CTRL = 0x30U; // bit0: battery voltage, bit2: VBUS voltage
|
||||
|
||||
static constexpr uint8_t BIT_VBUS_GOOD = 1U << 5U;
|
||||
static constexpr uint8_t BIT_SHUTDOWN = 1U << 0U;
|
||||
static constexpr uint8_t BIT_CHARGE_ENABLED = 1U << 1U;
|
||||
|
||||
static constexpr TickType_t TIMEOUT = pdMS_TO_TICKS(50);
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern Module axp2101_module;
|
||||
|
||||
// region Voltage encoding
|
||||
|
||||
struct Axp2101VoltRange {
|
||||
uint16_t min;
|
||||
uint16_t max;
|
||||
uint16_t step;
|
||||
uint8_t code_base;
|
||||
};
|
||||
|
||||
static error_t encode_ranged_voltage(uint16_t millivolts, const Axp2101VoltRange* ranges, size_t range_count, uint8_t* out_code) {
|
||||
for (size_t i = 0; i < range_count; i++) {
|
||||
const Axp2101VoltRange& range = ranges[i];
|
||||
if (millivolts >= range.min && millivolts <= range.max) {
|
||||
if ((millivolts - range.min) % range.step != 0U) {
|
||||
return ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
*out_code = static_cast<uint8_t>(range.code_base + (millivolts - range.min) / range.step);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
}
|
||||
return ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
static error_t write_masked_register(Device* device, uint8_t reg, uint8_t preserve_mask, uint8_t code) {
|
||||
auto* parent = device_get_parent(device);
|
||||
auto address = GET_CONFIG(device)->address;
|
||||
|
||||
if (preserve_mask == 0U) {
|
||||
return i2c_controller_register8_set(parent, address, reg, code, TIMEOUT);
|
||||
}
|
||||
|
||||
uint8_t value;
|
||||
error_t err = i2c_controller_register8_get(parent, address, reg, &value, TIMEOUT);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
value = static_cast<uint8_t>((value & preserve_mask) | code);
|
||||
return i2c_controller_register8_set(parent, address, reg, value, TIMEOUT);
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region DCDC
|
||||
|
||||
static error_t get_dcdc_enable_bit(Axp2101Dcdc dcdc, uint8_t* bit) {
|
||||
if (dcdc < AXP2101_DCDC1 || dcdc > AXP2101_DCDC5) {
|
||||
return ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
*bit = static_cast<uint8_t>(1U << dcdc);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t axp2101_is_dcdc_enabled(Device* device, Axp2101Dcdc dcdc, bool* enabled) {
|
||||
uint8_t bit;
|
||||
error_t err = get_dcdc_enable_bit(dcdc, &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_DC_ONOFF_DVM_CTRL, &value, TIMEOUT);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
|
||||
*enabled = (value & bit) != 0U;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t axp2101_set_dcdc_enabled(Device* device, Axp2101Dcdc dcdc, bool enabled) {
|
||||
uint8_t bit;
|
||||
error_t err = get_dcdc_enable_bit(dcdc, &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_DC_ONOFF_DVM_CTRL, bit, TIMEOUT);
|
||||
} else {
|
||||
return i2c_controller_register8_reset_bits(parent, address, REG_DC_ONOFF_DVM_CTRL, bit, TIMEOUT);
|
||||
}
|
||||
}
|
||||
|
||||
error_t axp2101_set_dcdc_voltage(Device* device, Axp2101Dcdc dcdc, uint16_t millivolts) {
|
||||
uint8_t reg = static_cast<uint8_t>(REG_DC_VOL0_CTRL + dcdc);
|
||||
uint8_t code;
|
||||
error_t err;
|
||||
|
||||
switch (dcdc) {
|
||||
case AXP2101_DCDC1: {
|
||||
static constexpr Axp2101VoltRange ranges[] = { { 1500, 3400, 100, 0 } };
|
||||
err = encode_ranged_voltage(millivolts, ranges, 1, &code);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
return write_masked_register(device, reg, 0x00U, code);
|
||||
}
|
||||
case AXP2101_DCDC2: {
|
||||
static constexpr Axp2101VoltRange ranges[] = { { 500, 1200, 10, 0 }, { 1220, 1540, 20, 71 } };
|
||||
err = encode_ranged_voltage(millivolts, ranges, 2, &code);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
return write_masked_register(device, reg, 0x80U, code);
|
||||
}
|
||||
case AXP2101_DCDC3: {
|
||||
static constexpr Axp2101VoltRange ranges[] = { { 500, 1200, 10, 0 }, { 1220, 1540, 20, 71 }, { 1600, 3400, 100, 88 } };
|
||||
err = encode_ranged_voltage(millivolts, ranges, 3, &code);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
return write_masked_register(device, reg, 0x80U, code);
|
||||
}
|
||||
case AXP2101_DCDC4: {
|
||||
static constexpr Axp2101VoltRange ranges[] = { { 500, 1200, 10, 0 }, { 1220, 1840, 20, 71 } };
|
||||
err = encode_ranged_voltage(millivolts, ranges, 2, &code);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
return write_masked_register(device, reg, 0x80U, code);
|
||||
}
|
||||
case AXP2101_DCDC5: {
|
||||
// DCDC5 datasheet quirk: 1200mV maps to a fixed out-of-sequence code, distinct
|
||||
// from the linear 1400-3700mV range (see XPowersLib's setDC5Voltage()).
|
||||
if (millivolts == 1200U) {
|
||||
return write_masked_register(device, reg, 0xE0U, 0x19U);
|
||||
}
|
||||
static constexpr Axp2101VoltRange ranges[] = { { 1400, 3700, 100, 0 } };
|
||||
err = encode_ranged_voltage(millivolts, ranges, 1, &code);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
return write_masked_register(device, reg, 0xE0U, code);
|
||||
}
|
||||
}
|
||||
return ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region LDO
|
||||
|
||||
static error_t get_ldo_enable_location(Axp2101Ldo ldo, uint8_t* reg, uint8_t* bit) {
|
||||
if (ldo < AXP2101_ALDO1 || ldo > AXP2101_DLDO2) {
|
||||
return ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
if (ldo == AXP2101_DLDO2) {
|
||||
*reg = REG_LDO_ONOFF_CTRL1;
|
||||
*bit = 1U << 0U;
|
||||
} else {
|
||||
*reg = REG_LDO_ONOFF_CTRL0;
|
||||
*bit = static_cast<uint8_t>(1U << ldo);
|
||||
}
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t axp2101_is_ldo_enabled(Device* device, Axp2101Ldo ldo, bool* enabled) {
|
||||
uint8_t reg, bit;
|
||||
error_t err = get_ldo_enable_location(ldo, ®, &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 axp2101_set_ldo_enabled(Device* device, Axp2101Ldo ldo, bool enabled) {
|
||||
uint8_t reg, bit;
|
||||
error_t err = get_ldo_enable_location(ldo, ®, &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 axp2101_set_ldo_voltage(Device* device, Axp2101Ldo ldo, uint16_t millivolts) {
|
||||
if (ldo < AXP2101_ALDO1 || ldo > AXP2101_DLDO2) {
|
||||
return ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
static constexpr Axp2101VoltRange LDO_RANGE[] = {
|
||||
{ 500, 3500, 100, 0 }, // ALDO1
|
||||
{ 500, 3500, 100, 0 }, // ALDO2
|
||||
{ 500, 3500, 100, 0 }, // ALDO3
|
||||
{ 500, 3500, 100, 0 }, // ALDO4
|
||||
{ 500, 3500, 100, 0 }, // BLDO1
|
||||
{ 500, 3500, 100, 0 }, // BLDO2
|
||||
{ 500, 1400, 50, 0 }, // CPUSLDO
|
||||
{ 500, 3400, 100, 0 }, // DLDO1
|
||||
{ 500, 3400, 100, 0 }, // DLDO2
|
||||
};
|
||||
|
||||
uint8_t code;
|
||||
error_t err = encode_ranged_voltage(millivolts, &LDO_RANGE[ldo], 1, &code);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
|
||||
uint8_t reg = static_cast<uint8_t>(REG_LDO_VOL0_CTRL + ldo);
|
||||
return write_masked_register(device, reg, 0xE0U, code);
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
error_t axp2101_get_battery_voltage(Device* device, uint16_t* millivolts) {
|
||||
bool connected;
|
||||
error_t err = axp2101_is_battery_connected(device, &connected);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
if (!connected) {
|
||||
*millivolts = 0;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
auto* parent = device_get_parent(device);
|
||||
auto address = GET_CONFIG(device)->address;
|
||||
uint8_t high, low;
|
||||
err = i2c_controller_register8_get(parent, address, REG_ADC_DATA_RESULT0, &high, TIMEOUT);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
err = i2c_controller_register8_get(parent, address, REG_ADC_DATA_RESULT1, &low, TIMEOUT);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
*millivolts = static_cast<uint16_t>(((high & 0x1FU) << 8U) | low);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t axp2101_is_battery_connected(Device* device, bool* connected) {
|
||||
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_STATUS1, &value, TIMEOUT);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
*connected = (value & (1U << 3U)) != 0U;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t axp2101_is_vbus_present(Device* device, bool* present) {
|
||||
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_STATUS1, &value, TIMEOUT);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
*present = (value & BIT_VBUS_GOOD) != 0U;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t axp2101_get_vbus_voltage(Device* device, uint16_t* millivolts) {
|
||||
bool present;
|
||||
error_t err = axp2101_is_vbus_present(device, &present);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
if (!present) {
|
||||
*millivolts = 0;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
auto* parent = device_get_parent(device);
|
||||
auto address = GET_CONFIG(device)->address;
|
||||
uint8_t high, low;
|
||||
err = i2c_controller_register8_get(parent, address, REG_ADC_DATA_RESULT4, &high, TIMEOUT);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
err = i2c_controller_register8_get(parent, address, REG_ADC_DATA_RESULT5, &low, TIMEOUT);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
*millivolts = static_cast<uint16_t>(((high & 0x3FU) << 8U) | low);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t axp2101_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_STATUS2, &value, TIMEOUT);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
*charging = ((value >> 5U) & 0x03U) == 0x01U;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t axp2101_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_GAUGE_WDT_CTRL, &value, TIMEOUT);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
*enabled = (value & BIT_CHARGE_ENABLED) != 0U;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t axp2101_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_GAUGE_WDT_CTRL, BIT_CHARGE_ENABLED, TIMEOUT);
|
||||
} else {
|
||||
return i2c_controller_register8_reset_bits(parent, address, REG_CHARGE_GAUGE_WDT_CTRL, BIT_CHARGE_ENABLED, TIMEOUT);
|
||||
}
|
||||
}
|
||||
|
||||
error_t axp2101_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_COMMON_CONFIG, BIT_SHUTDOWN, 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;
|
||||
}
|
||||
|
||||
static error_t ps_get_property(Device* device, PowerSupplyProperty property, PowerSupplyPropertyValue* out_value) {
|
||||
// device_get_parent() here is the axp2101 device itself (this child's parent), not the I2C bus.
|
||||
auto* axp2101_device = device_get_parent(device);
|
||||
|
||||
switch (property) {
|
||||
case POWER_SUPPLY_PROP_IS_CHARGING: {
|
||||
bool charging;
|
||||
error_t err = axp2101_is_charging(axp2101_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 = axp2101_get_battery_voltage(axp2101_device, &millivolts);
|
||||
if (err != ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
out_value->int_value = millivolts;
|
||||
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;
|
||||
axp2101_is_charge_enabled(device_get_parent(device), &enabled);
|
||||
return enabled;
|
||||
}
|
||||
|
||||
static error_t ps_set_allowed_to_charge(Device* device, bool allowed) {
|
||||
return axp2101_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 axp2101_power_off(device_get_parent(device)); }
|
||||
|
||||
static constexpr PowerSupplyApi AXP2101_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: axp2101_driver wires it up directly by pointer.
|
||||
Driver axp2101_power_supply_driver = {
|
||||
.name = "axp2101-power-supply",
|
||||
.compatible = (const char*[]) { "axp2101-power-supply", nullptr },
|
||||
.start_device = nullptr,
|
||||
.stop_device = nullptr,
|
||||
.api = &AXP2101_POWER_SUPPLY_API,
|
||||
.device_type = &POWER_SUPPLY_TYPE,
|
||||
.owner = &axp2101_module,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
struct Axp2101Internal {
|
||||
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 = "axp2101-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, &axp2101_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 address = GET_CONFIG(device)->address;
|
||||
// Battery/VBUS voltage ADC channels are off by default; axp2101_get_battery_voltage()
|
||||
// and axp2101_get_vbus_voltage() need them on to read anything but 0.
|
||||
error_t error = i2c_controller_register8_set_bits(parent, address, REG_ADC_CHANNEL_CTRL, (1U << 0U) | (1U << 2U), TIMEOUT);
|
||||
if (error != ERROR_NONE) {
|
||||
LOG_W(TAG, "Failed to enable battery/VBUS ADC channels");
|
||||
return error;
|
||||
}
|
||||
|
||||
auto* internal = new(std::nothrow) Axp2101Internal();
|
||||
if (internal == nullptr) {
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
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<Axp2101Internal*>(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 axp2101_driver = {
|
||||
.name = "axp2101",
|
||||
.compatible = (const char*[]) { "x-powers,axp2101", nullptr },
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = nullptr,
|
||||
.device_type = nullptr,
|
||||
.owner = &axp2101_module,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <drivers/axp2101_backlight.h>
|
||||
#include <axp2101_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>
|
||||
|
||||
#define TAG "Axp2101Backlight"
|
||||
#define GET_CONFIG(device) (static_cast<const Axp2101BacklightConfig*>((device)->config))
|
||||
#define GET_INTERNAL(device) (static_cast<Axp2101BacklightInternal*>(device_get_driver_data(device)))
|
||||
|
||||
extern "C" {
|
||||
|
||||
struct Axp2101BacklightInternal {
|
||||
uint8_t brightness;
|
||||
};
|
||||
|
||||
// region BacklightApi
|
||||
|
||||
// Step size of axp2101_set_ldo_voltage()'s underlying register encoding (see axp2101.cpp's LDO_RANGE table).
|
||||
static uint16_t get_ldo_voltage_step(Axp2101Ldo ldo) {
|
||||
return ldo == AXP2101_CPUSLDO ? 50U : 100U;
|
||||
}
|
||||
|
||||
static error_t apply_brightness(Device* device, uint8_t brightness) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
auto* axp2101 = device_get_parent(device);
|
||||
|
||||
if (brightness == 0) {
|
||||
return axp2101_set_ldo_enabled(axp2101, config->ldo, false);
|
||||
}
|
||||
|
||||
uint16_t step = get_ldo_voltage_step(config->ldo);
|
||||
uint16_t raw_millivolt = static_cast<uint16_t>(
|
||||
config->min_millivolt +
|
||||
(static_cast<uint32_t>(brightness) * (config->max_millivolt - config->min_millivolt)) / 255U
|
||||
);
|
||||
// Round to the nearest valid step; the LDO's voltage range always starts at a multiple of every
|
||||
// supported step, so rounding from zero keeps the result on a valid boundary.
|
||||
uint16_t millivolt = static_cast<uint16_t>(((raw_millivolt + step / 2U) / step) * step);
|
||||
if (millivolt < config->min_millivolt) {
|
||||
millivolt = config->min_millivolt;
|
||||
} else if (millivolt > config->max_millivolt) {
|
||||
millivolt = config->max_millivolt;
|
||||
}
|
||||
|
||||
error_t error = axp2101_set_ldo_voltage(axp2101, config->ldo, millivolt);
|
||||
if (error != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to set LDO voltage");
|
||||
return error;
|
||||
}
|
||||
|
||||
return axp2101_set_ldo_enabled(axp2101, config->ldo, true);
|
||||
}
|
||||
|
||||
static error_t axp2101_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 axp2101_backlight_set_brightness_default(Device* device) {
|
||||
return axp2101_backlight_set_brightness(device, GET_CONFIG(device)->brightness_default);
|
||||
}
|
||||
|
||||
static error_t axp2101_backlight_get_brightness(Device* device, uint8_t* out_brightness) {
|
||||
*out_brightness = GET_INTERNAL(device)->brightness;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static uint8_t axp2101_backlight_get_min_brightness(Device*) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint8_t axp2101_backlight_get_max_brightness(Device*) {
|
||||
return 255;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
static constexpr BacklightApi AXP2101_BACKLIGHT_API = {
|
||||
.set_brightness = axp2101_backlight_set_brightness,
|
||||
.set_brightness_default = axp2101_backlight_set_brightness_default,
|
||||
.get_brightness = axp2101_backlight_get_brightness,
|
||||
.get_min_brightness = axp2101_backlight_get_min_brightness,
|
||||
.get_max_brightness = axp2101_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) Axp2101BacklightInternal { .brightness = 0 };
|
||||
if (internal == nullptr) {
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
device_set_driver_data(device, internal);
|
||||
|
||||
axp2101_backlight_set_brightness_default(device); // Allowed to fail, we don't care about the result
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop(Device* device) {
|
||||
axp2101_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 axp2101_backlight_driver = {
|
||||
.name = "axp2101_backlight",
|
||||
.compatible = (const char*[]) { "axp2101-backlight", nullptr },
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = &AXP2101_BACKLIGHT_API,
|
||||
.device_type = &BACKLIGHT_TYPE,
|
||||
.owner = &axp2101_module,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <drivers/axp2101.h>
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern Driver axp2101_driver;
|
||||
extern Driver axp2101_power_supply_driver;
|
||||
extern Driver axp2101_backlight_driver;
|
||||
|
||||
const struct ModuleSymbol axp2101_module_symbols[] = {
|
||||
DEFINE_MODULE_SYMBOL(axp2101_is_dcdc_enabled),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_set_dcdc_enabled),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_set_dcdc_voltage),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_is_ldo_enabled),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_set_ldo_enabled),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_set_ldo_voltage),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_get_battery_voltage),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_is_battery_connected),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_is_vbus_present),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_get_vbus_voltage),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_is_charging),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_is_charge_enabled),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_set_charge_enabled),
|
||||
DEFINE_MODULE_SYMBOL(axp2101_power_off),
|
||||
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(&axp2101_driver) == ERROR_NONE);
|
||||
check(driver_construct_add(&axp2101_power_supply_driver) == ERROR_NONE);
|
||||
check(driver_construct_add(&axp2101_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(&axp2101_backlight_driver) == ERROR_NONE);
|
||||
check(driver_remove_destruct(&axp2101_power_supply_driver) == ERROR_NONE);
|
||||
check(driver_remove_destruct(&axp2101_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
Module axp2101_module = {
|
||||
.name = "axp2101",
|
||||
.start = start,
|
||||
.stop = stop,
|
||||
.symbols = axp2101_module_symbols,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user