drivers: power_switch: add PWM support

Extend the power switch API for switches driven by a PWM signal, as used
in PWM solar charge controllers:

- Set and get the duty cycle
- Check if the switch is currently conducting (momentary PWM state)
- Generic event callback replacing the fault callback, which also
  reports the rising edges of the PWM signal

New backends:

- libresolar,stm32-pwm-power-switch: PWM signal generated by a channel
  of an STM32 timer. The output is driven inactive while switched off
  instead of disabling the channel, which left the pin floating.
- libresolar,power-switch-emul: Emulated switch for tests on native_sim
  supporting all features of the API. Power-good failures and events
  can be injected by the test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Jäger
2026-11-01 14:20:21 +01:00
co-authored by Claude Opus 5.5
parent 0adc9edb7a
commit da59ab8276
17 changed files with 769 additions and 26 deletions
+115 -13
View File
@@ -15,12 +15,17 @@
* switching itself, drivers may provide a power-good feedback from the switch or its gate driver
* and report faults detected in hardware, e.g. a short circuit that already switched off the
* output without any software interaction.
*
* Switches supporting PWM (e.g. used in PWM solar charge controllers) are switched on and off
* periodically with the configured duty cycle while they are on.
*/
#include <zephyr/device.h>
#include <zephyr/sys/util.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
@@ -32,14 +37,35 @@ extern "C" {
*/
/**
* Callback for faults detected and handled by the hardware
* Power switch events
*/
enum power_switch_event
{
/**
* Fault detected and handled by the hardware (e.g. short circuit)
*
* The switch is already off when the callback is called.
*/
POWER_SWITCH_EVENT_FAULT = BIT(0),
/**
* Rising edge of the PWM signal, i.e. the switch starts conducting
*
* Not generated if the duty cycle is 0% or 100%.
*/
POWER_SWITCH_EVENT_PWM_EDGE = BIT(1),
};
/**
* Callback for power switch events
*
* The switch is already off when the callback is called. It may be called from an ISR.
* The callback may be called from an ISR.
*
* @param dev Power switch device
* @param user_data User data provided with power_switch_set_fault_callback()
* @param events Bitmask of events according to enum power_switch_event
* @param user_data User data provided with power_switch_set_callback()
*/
typedef void (*power_switch_fault_callback_t)(const struct device *dev, void *user_data);
typedef void (*power_switch_callback_t)(const struct device *dev, uint32_t events, void *user_data);
/** @cond INTERNAL_HIDDEN */
@@ -47,9 +73,12 @@ __subsystem struct power_switch_driver_api
{
int (*set)(const struct device *dev, bool on);
bool (*is_on)(const struct device *dev);
bool (*is_conducting)(const struct device *dev);
int (*power_good)(const struct device *dev);
int (*set_fault_callback)(const struct device *dev, power_switch_fault_callback_t callback,
void *user_data);
int (*set_duty_cycle)(const struct device *dev, float duty);
float (*get_duty_cycle)(const struct device *dev);
int (*set_callback)(const struct device *dev, uint32_t events, power_switch_callback_t callback,
void *user_data);
};
/** @endcond */
@@ -96,6 +125,29 @@ static inline bool power_switch_is_on(const struct device *dev)
return DEVICE_API_GET(power_switch, dev)->is_on(dev);
}
/**
* Check if the switch is currently conducting
*
* For switches with PWM, this is the momentary state of the PWM signal. For other switches, it is
* the same as power_switch_is_on().
*
* Can be called from an ISR.
*
* @param dev Power switch device
*
* @returns true if the switch is conducting
*/
static inline bool power_switch_is_conducting(const struct device *dev)
{
const struct power_switch_driver_api *api = DEVICE_API_GET(power_switch, dev);
if (api->is_conducting == NULL) {
return api->is_on(dev);
}
return api->is_conducting(dev);
}
/**
* Check the power-good feedback of the switch
*
@@ -117,26 +169,76 @@ static inline int power_switch_power_good(const struct device *dev)
}
/**
* Set callback for faults detected and handled by the hardware (e.g. short circuit)
* Set the PWM duty cycle
*
* The duty cycle can be set before switching on and is kept while the switch is off.
*
* @param dev Power switch device
* @param duty Duty cycle between 0.0 and 1.0
*
* @retval 0 on success
* @retval -EINVAL if duty cycle is out of range (setting is ignored)
* @retval -ENOSYS if the switch does not support PWM
*/
static inline int power_switch_set_duty_cycle(const struct device *dev, float duty)
{
const struct power_switch_driver_api *api = DEVICE_API_GET(power_switch, dev);
if (api->set_duty_cycle == NULL) {
return -ENOSYS;
}
if (duty < 0.0F || duty > 1.0F) {
return -EINVAL;
}
return api->set_duty_cycle(dev, duty);
}
/**
* Get the PWM duty cycle
*
* For switches without PWM support, the duty cycle is 1.0 while the switch is on.
*
* @param dev Power switch device
*
* @returns Duty cycle between 0.0 and 1.0
*/
static inline float power_switch_get_duty_cycle(const struct device *dev)
{
const struct power_switch_driver_api *api = DEVICE_API_GET(power_switch, dev);
if (api->get_duty_cycle == NULL) {
return api->is_on(dev) ? 1.0F : 0.0F;
}
return api->get_duty_cycle(dev);
}
/**
* Set callback for power switch events
*
* Only a single callback is supported. A new callback replaces the previous one.
*
* @param dev Power switch device
* @param events Bitmask of events (enum power_switch_event) the callback should be called for
* @param callback Callback function or NULL to remove the callback
* @param user_data User data passed to the callback
*
* @retval 0 on success
* @retval -ENOSYS if the switch does not detect faults in hardware
* @retval -ENOTSUP if one of the requested events is not supported by the switch
* @retval -ENOSYS if the switch does not support any events
*/
static inline int power_switch_set_fault_callback(const struct device *dev,
power_switch_fault_callback_t callback,
void *user_data)
static inline int power_switch_set_callback(const struct device *dev, uint32_t events,
power_switch_callback_t callback, void *user_data)
{
const struct power_switch_driver_api *api = DEVICE_API_GET(power_switch, dev);
if (api->set_fault_callback == NULL) {
if (api->set_callback == NULL) {
return -ENOSYS;
}
return api->set_fault_callback(dev, callback, user_data);
return api->set_callback(dev, events, callback, user_data);
}
/** @} */
@@ -0,0 +1,46 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef LIBRESOLAR_DRIVERS_POWER_SWITCH_EMUL_H_
#define LIBRESOLAR_DRIVERS_POWER_SWITCH_EMUL_H_
/**
* @file
* @brief Backend API of the emulated power switch for tests
*/
#include <zephyr/device.h>
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Set the power-good feedback reported while the switch is on
*
* @param dev Emulated power switch device
* @param power_good Power-good state
*/
void power_switch_emul_set_power_good(const struct device *dev, bool power_good);
/**
* Trigger events as if they were detected by the hardware
*
* A fault event switches off the output before the callback is called.
*
* @param dev Emulated power switch device
* @param events Bitmask of events according to enum power_switch_event
*/
void power_switch_emul_trigger_events(const struct device *dev, uint32_t events);
#ifdef __cplusplus
}
#endif
#endif /* LIBRESOLAR_DRIVERS_POWER_SWITCH_EMUL_H_ */