Skip to content

Commit

Permalink
charger: Initial charger dedicated API
Browse files Browse the repository at this point in the history
Add initial charger driver API with the most basic of native_posix
driver tests.

Signed-off-by: Ricardo Rivera-Matos <rriveram@opensource.cirrus.com>
  • Loading branch information
rriveramcrus committed Aug 22, 2023
1 parent 20e7c6d commit 1afad57
Show file tree
Hide file tree
Showing 2 changed files with 245 additions and 0 deletions.
44 changes: 44 additions & 0 deletions drivers/charger/charger_handlers.c
@@ -0,0 +1,44 @@
/*
* Copyright 2023 Cirrus Logic, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/syscall_handler.h>
#include <zephyr/drivers/charger.h>

static inline int z_vrfy_charger_get_prop(const struct device *dev,
struct charger_get_property *prop)
{
struct charger_get_property k_prop;

Z_OOPS(Z_SYSCALL_DRIVER_CHARGER(dev, get_property));

Z_OOPS(z_user_from_copy(&k_prop, prop, sizeof(struct charger_get_property)));

int ret = z_impl_charger_get_prop(dev, &k_prop);

Z_OOPS(z_user_to_copy(prop, &k_prop, sizeof(struct charger_get_property)));

return ret;
}

#include <syscalls/charger_get_prop_mrsh.c>

static inline int z_vrfy_charger_set_prop(const struct device *dev,
struct charger_set_property *prop)
{
struct charger_set_property k_prop;

Z_OOPS(Z_SYSCALL_DRIVER_CHARGER(dev, set_property));

Z_OOPS(z_user_from_copy(&k_prop, prop, sizeof(struct charger_set_property)));

int ret = z_impl_charger_set_prop(dev, &k_prop);

Z_OOPS(z_user_to_copy(prop, &k_prop, sizeof(struct charger_set_property)));

return ret;
}

#include <syscalls/charger_set_prop_mrsh.c>
201 changes: 201 additions & 0 deletions include/zephyr/drivers/charger.h
@@ -0,0 +1,201 @@
/*
* Copyright 2023 Cirrus Logic, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_DRIVERS_CHARGER_H_
#define ZEPHYR_INCLUDE_DRIVERS_CHARGER_H_

/**
* @brief Charger Interface
* @defgroup charger_interface Charger Interface
* @ingroup io_interfaces
* @{
*/

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include <zephyr/device.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/**
* @brief Runtime Dynamic Battery Parameters
*/
enum charger_property {
/** Indicates if external supply is present for the charger. */
/** Value should be of type enum charger_online */
CHARGER_PROP_ONLINE = 0,
/** Reports whether or not a battery is present. */
/** Value should be of type bool*/
CHARGER_PROP_PRESENT,
/** Represents the charging status of the charger. */
/** Value should be of type enum charger_status */
CHARGER_PROP_STATUS,
/** Reserved to demark end of common charger properties */
CHARGER_PROP_COMMON_COUNT,
/**
* Reserved to demark downstream custom properties - use this value as the actual value may
* change over future versions of this API
*/
CHARGER_PROP_CUSTOM_BEGIN = CHARGER_PROP_COMMON_COUNT + 1,
/** Reserved to demark end of valid enum properties */
CHARGER_PROP_MAX = UINT16_MAX,
};

/**
* @typedef charger_prop_t
* @brief A charger property's identifier
*
* See charger_property for a list of identifiers
*/
typedef uint16_t charger_prop_t;

/**
* @brief External supply states
*/
enum charger_online {
/** External supply not present */
CHARGER_ONLINE_OFFLINE = 0,
/** External supply is present and of fixed output */
CHARGER_ONLINE_FIXED,
/** External supply is present and of programmable output*/
CHARGER_ONLINE_PROGRAMMABLE,
};

/**
* @brief Charging states
*/
enum charger_status {
/** Charging device state is unknown */
CHARGER_STATUS_UNKNOWN = 0,
/** Charging device is charging a battery */
CHARGER_STATUS_CHARGING,
/** Charging device is not able to charge a battery */
CHARGER_STATUS_DISCHARGING,
/** Charging device is not charging a battery */
CHARGER_STATUS_NOT_CHARGING,
/** The battery is full and the charging device will not attempt charging */
CHARGER_STATUS_FULL,
};

struct charger_get_property {
/** Battery charger property to get */
charger_prop_t property_type;

/** Property field for getting */
union {
/* Fields have the format: */
/* CHARGER_PROPERTY_FIELD */
/* type property_field; */

/* Dynamic Battery Info */
/** CHARGER_PROP_ONLINE */
enum charger_online online;
/** CHARGER_PROP_PRESENT */
bool present;
/** CHARGER_PROP_STATUS */
enum charger_status status;
} value;
};

struct charger_set_property {
/** Battery charger property to set */
charger_prop_t property_type;

/** Property field for getting */
union {
/* Fields have the format: */
/* CHARGER_PROPERTY_FIELD */
/* type property_field; */

/* Dynamic Battery Info */
/** CHARGER_PROP_ONLINE */
enum charger_online online;
/** CHARGER_PROP_PRESENT */
bool present;
/** CHARGER_PROP_STATUS */
enum charger_status status;
} value;
};

/**
* @typedef charger_get_property_t
* @brief Callback API for getting a charger property.
*
* See charger_get_property() for argument description
*/
typedef int (*charger_get_property_t)(const struct device *dev, struct charger_get_property *prop);

/**
* @typedef charger_set_property_t
* @brief Callback API for setting a charger property.
*
* See charger_set_property() for argument description
*/
typedef int (*charger_set_property_t)(const struct device *dev, struct charger_set_property *prop);

/**
* @brief Charging device API
*
* Caching is entirely on the onus of the client
*/
__subsystem struct charger_driver_api {
charger_get_property_t get_property;
charger_set_property_t set_property;
};

/**
* @brief Fetch a battery charger property
*
* @param dev Pointer to the battery charger device
* @param prop Pointer to the charger_get_property struct
*
* @retval 0 if successful
* @retval < 0 if getting property failed
*/
__syscall int charger_get_prop(const struct device *dev, struct charger_get_property *prop);

static inline int z_impl_charger_get_prop(const struct device *dev,
struct charger_get_property *prop)
{
const struct charger_driver_api *api = (const struct charger_driver_api *)dev->api;

return api->get_property(dev, prop);
}

/**
* @brief Set a battery charger property
*
* @param dev Pointer to the battery charger device
* @param prop Pointer to the charger_set_property struct
*
* @retval 0 if successful
* @retval < 0 if setting property failed
*/
__syscall int charger_set_prop(const struct device *dev, struct charger_set_property *prop);

static inline int z_impl_charger_set_prop(const struct device *dev,
struct charger_set_property *prop)
{
const struct charger_driver_api *api = (const struct charger_driver_api *)dev->api;

return api->set_property(dev, prop);
}

/**
* @}
*/

#ifdef __cplusplus
}
#endif /* __cplusplus */

#include <syscalls/charger.h>

#endif /* ZEPHYR_INCLUDE_DRIVERS_CHARGER_H_ */

0 comments on commit 1afad57

Please sign in to comment.