drivers: regulator: convert all API calls to use ops

The previous "consumer" API was designed without API ops, meaning only a
single implementation (singleton) was possible. This was inconsistent
with all other APIs, and, a problem in systems where more than a single
PMIC IC is present.

Note that all "consumer" ops are optional, so this won't break any
existing drivers.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-11-11 12:35:12 +01:00 committed by Carles Cufí
commit ec3c566f8c
2 changed files with 213 additions and 38 deletions

View file

@ -62,6 +62,9 @@ struct regulator_config {
uint8_t modesel_mask; uint8_t modesel_mask;
}; };
static int regulator_pmic_is_supported_voltage(const struct device *dev,
int min_uV, int max_uV);
/** /**
* Reads a register from the PMIC * Reads a register from the PMIC
* Returns 0 on success, or errno on error * Returns 0 on success, or errno on error
@ -141,7 +144,7 @@ static int regulator_set_voltage_offset(const struct device *dev, int min_uV,
struct regulator_data *data = dev->data; struct regulator_data *data = dev->data;
int i = 0; int i = 0;
if (!regulator_is_supported_voltage(dev, min_uV, max_uV) || if (!regulator_pmic_is_supported_voltage(dev, min_uV, max_uV) ||
min_uV > max_uV) { min_uV > max_uV) {
return -EINVAL; return -EINVAL;
} }
@ -169,7 +172,7 @@ static int regulator_set_voltage_offset(const struct device *dev, int min_uV,
* Part of the extended regulator consumer API * Part of the extended regulator consumer API
* Returns the number of supported voltages * Returns the number of supported voltages
*/ */
int regulator_count_voltages(const struct device *dev) static int regulator_pmic_count_voltages(const struct device *dev)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
@ -180,7 +183,7 @@ int regulator_count_voltages(const struct device *dev)
* Part of the extended regulator consumer API * Part of the extended regulator consumer API
* Counts the number of modes supported by a regulator * Counts the number of modes supported by a regulator
*/ */
int regulator_count_modes(const struct device *dev) static int regulator_pmic_count_modes(const struct device *dev)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
@ -191,7 +194,8 @@ int regulator_count_modes(const struct device *dev)
* Part of the extended regulator consumer API * Part of the extended regulator consumer API
* Returns the supported voltage in uV for a given selector value * Returns the supported voltage in uV for a given selector value
*/ */
int regulator_list_voltages(const struct device *dev, unsigned int selector) static int regulator_pmic_list_voltages(const struct device *dev,
unsigned int selector)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data; struct regulator_data *data = dev->data;
@ -206,8 +210,8 @@ int regulator_list_voltages(const struct device *dev, unsigned int selector)
* Part of the extended regulator consumer API * Part of the extended regulator consumer API
* Returns true if the regulator supports a voltage in the given range. * Returns true if the regulator supports a voltage in the given range.
*/ */
int regulator_is_supported_voltage(const struct device *dev, static int regulator_pmic_is_supported_voltage(const struct device *dev,
int min_uV, int max_uV) int min_uV, int max_uV)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
@ -218,7 +222,8 @@ int regulator_is_supported_voltage(const struct device *dev,
* Part of the extended regulator consumer API * Part of the extended regulator consumer API
* Sets the output voltage to the closest supported voltage value * Sets the output voltage to the closest supported voltage value
*/ */
int regulator_set_voltage(const struct device *dev, int min_uV, int max_uV) static int regulator_pmic_set_voltage(const struct device *dev, int min_uV,
int max_uV)
{ {
return regulator_set_voltage_offset(dev, min_uV, max_uV, 0); return regulator_set_voltage_offset(dev, min_uV, max_uV, 0);
} }
@ -228,7 +233,7 @@ int regulator_set_voltage(const struct device *dev, int min_uV, int max_uV)
* Part of the extended regulator consumer API * Part of the extended regulator consumer API
* Gets the current output voltage in uV * Gets the current output voltage in uV
*/ */
int regulator_get_voltage(const struct device *dev) static int regulator_pmic_get_voltage(const struct device *dev)
{ {
return regulator_get_voltage_offset(dev, 0); return regulator_get_voltage_offset(dev, 0);
} }
@ -237,7 +242,8 @@ int regulator_get_voltage(const struct device *dev)
* Part of the extended regulator consumer API * Part of the extended regulator consumer API
* Set the current limit for this device * Set the current limit for this device
*/ */
int regulator_set_current_limit(const struct device *dev, int min_uA, int max_uA) static int regulator_pmic_set_current_limit(const struct device *dev, int min_uA,
int max_uA)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data; struct regulator_data *data = dev->data;
@ -265,7 +271,7 @@ int regulator_set_current_limit(const struct device *dev, int min_uA, int max_uA
* Part of the extended regulator consumer API * Part of the extended regulator consumer API
* Gets the set current limit for the regulator * Gets the set current limit for the regulator
*/ */
int regulator_get_current_limit(const struct device *dev) static int regulator_pmic_get_current_limit(const struct device *dev)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data; struct regulator_data *data = dev->data;
@ -295,10 +301,11 @@ int regulator_get_current_limit(const struct device *dev)
* sets the target voltage for a given regulator mode. This mode does * sets the target voltage for a given regulator mode. This mode does
* not need to be the active mode. This API can be used to configure * not need to be the active mode. This API can be used to configure
* voltages for a mode, then the regulator can be switched to that mode * voltages for a mode, then the regulator can be switched to that mode
* with the regulator_set_mode api * with the regulator_pmic_set_mode api
*/ */
int regulator_set_mode_voltage(const struct device *dev, uint32_t mode, static int regulator_pmic_set_mode_voltage(const struct device *dev,
uint32_t min_uV, uint32_t max_uV) uint32_t mode, uint32_t min_uV,
uint32_t max_uV)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
uint8_t i, sel_off; uint8_t i, sel_off;
@ -326,7 +333,7 @@ int regulator_set_mode_voltage(const struct device *dev, uint32_t mode,
* Disables the regulator in a given mode. Does not implement the * Disables the regulator in a given mode. Does not implement the
* onoff service, as this is incompatible with multiple mode operation * onoff service, as this is incompatible with multiple mode operation
*/ */
int regulator_mode_disable(const struct device *dev, uint32_t mode) static int regulator_pmic_mode_disable(const struct device *dev, uint32_t mode)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
uint8_t i, sel_off, dis_val; uint8_t i, sel_off, dis_val;
@ -356,7 +363,7 @@ int regulator_mode_disable(const struct device *dev, uint32_t mode)
* Enables the regulator in a given mode. Does not implement the * Enables the regulator in a given mode. Does not implement the
* onoff service, as this is incompatible with multiple mode operation * onoff service, as this is incompatible with multiple mode operation
*/ */
int regulator_mode_enable(const struct device *dev, uint32_t mode) static int regulator_pmic_mode_enable(const struct device *dev, uint32_t mode)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
uint8_t i, sel_off, en_val; uint8_t i, sel_off, en_val;
@ -387,7 +394,8 @@ int regulator_mode_enable(const struct device *dev, uint32_t mode)
* not need to be the active mode. This API can be used to read voltages * not need to be the active mode. This API can be used to read voltages
* from a regulator mode other than the default. * from a regulator mode other than the default.
*/ */
int regulator_get_mode_voltage(const struct device *dev, uint32_t mode) static int regulator_pmic_get_mode_voltage(const struct device *dev,
uint32_t mode)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
uint8_t i, sel_off; uint8_t i, sel_off;
@ -415,7 +423,7 @@ int regulator_get_mode_voltage(const struct device *dev, uint32_t mode)
* switches the regulator to a given mode. This API will apply a mode for * switches the regulator to a given mode. This API will apply a mode for
* the regulator. * the regulator.
*/ */
int regulator_set_mode(const struct device *dev, uint32_t mode) static int regulator_pmic_set_mode(const struct device *dev, uint32_t mode)
{ {
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
int rc; int rc;
@ -450,7 +458,7 @@ int regulator_set_mode(const struct device *dev, uint32_t mode)
return rc; return rc;
} }
static int enable_regulator(const struct device *dev, struct onoff_client *cli) static int regulator_pmic_enable(const struct device *dev, struct onoff_client *cli)
{ {
k_spinlock_key_t key; k_spinlock_key_t key;
int rc; int rc;
@ -473,7 +481,7 @@ static int enable_regulator(const struct device *dev, struct onoff_client *cli)
return onoff_sync_finalize(&data->srv, key, cli, rc, true); return onoff_sync_finalize(&data->srv, key, cli, rc, true);
} }
static int disable_regulator(const struct device *dev) static int regulator_pmic_disable(const struct device *dev)
{ {
struct regulator_data *data = dev->data; struct regulator_data *data = dev->data;
const struct regulator_config *config = dev->config; const struct regulator_config *config = dev->config;
@ -512,21 +520,33 @@ static int pmic_reg_init(const struct device *dev)
return -ENODEV; return -ENODEV;
} }
if (config->boot_on) { if (config->boot_on) {
rc = enable_regulator(dev, NULL); rc = regulator_pmic_enable(dev, NULL);
} }
if (config->initial_mode) { if (config->initial_mode) {
rc = regulator_set_mode(dev, config->initial_mode); rc = regulator_pmic_set_mode(dev, config->initial_mode);
} }
return rc; return rc;
} }
static const struct regulator_driver_api api = { static const struct regulator_driver_api api = {
.enable = enable_regulator, .enable = regulator_pmic_enable,
.disable = disable_regulator .disable = regulator_pmic_disable,
.count_voltages = regulator_pmic_count_voltages,
.count_modes = regulator_pmic_count_modes,
.list_voltages = regulator_pmic_list_voltages,
.is_supported_voltage = regulator_pmic_is_supported_voltage,
.set_voltage = regulator_pmic_set_voltage,
.get_voltage = regulator_pmic_get_voltage,
.set_current_limit = regulator_pmic_set_current_limit,
.get_current_limit = regulator_pmic_get_current_limit,
.set_mode = regulator_pmic_set_mode,
.set_mode_voltage = regulator_pmic_set_mode_voltage,
.get_mode_voltage = regulator_pmic_get_mode_voltage,
.mode_disable = regulator_pmic_mode_disable,
.mode_enable = regulator_pmic_mode_enable,
}; };
/* /*
* Each regulator output will be initialized as a separate device struct, * Each regulator output will be initialized as a separate device struct,
* and implement the regulator API. Since the DT binding is defined for the * and implement the regulator API. Since the DT binding is defined for the

View file

@ -15,6 +15,7 @@
* @{ * @{
*/ */
#include <errno.h>
#include <stdint.h> #include <stdint.h>
#include <zephyr/device.h> #include <zephyr/device.h>
@ -30,6 +31,22 @@ extern "C" {
__subsystem struct regulator_driver_api { __subsystem struct regulator_driver_api {
int (*enable)(const struct device *dev, struct onoff_client *cli); int (*enable)(const struct device *dev, struct onoff_client *cli);
int (*disable)(const struct device *dev); int (*disable)(const struct device *dev);
int (*count_voltages)(const struct device *dev);
int (*count_modes)(const struct device *dev);
int (*list_voltages)(const struct device *dev, unsigned int selector);
int (*is_supported_voltage)(const struct device *dev, int min_uV,
int max_uV);
int (*set_voltage)(const struct device *dev, int min_uV, int max_uV);
int (*get_voltage)(const struct device *dev);
int (*set_current_limit)(const struct device *dev, int min_uA,
int max_uA);
int (*get_current_limit)(const struct device *dev);
int (*set_mode)(const struct device *dev, uint32_t mode);
int (*set_mode_voltage)(const struct device *dev, uint32_t mode,
uint32_t min_uV, uint32_t max_uV);
int (*get_mode_voltage)(const struct device *dev, uint32_t mode);
int (*mode_disable)(const struct device *dev, uint32_t mode);
int (*mode_enable)(const struct device *dev, uint32_t mode);
}; };
/** @endcond */ /** @endcond */
@ -99,7 +116,17 @@ static inline int regulator_disable(const struct device *dev)
* @param dev: Regulator device to count voltage levels for. * @param dev: Regulator device to count voltage levels for.
* @return number of selectors, or negative errno. * @return number of selectors, or negative errno.
*/ */
int regulator_count_voltages(const struct device *dev); static inline int regulator_count_voltages(const struct device *dev)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->count_voltages == NULL) {
return -ENOSYS;
}
return api->count_voltages(dev);
}
/** /**
* @brief Return the number of supported regulator modes * @brief Return the number of supported regulator modes
@ -110,7 +137,17 @@ int regulator_count_voltages(const struct device *dev);
* @param dev: Regulator device to count supported regulator modes for * @param dev: Regulator device to count supported regulator modes for
* @return number of supported modes * @return number of supported modes
*/ */
int regulator_count_modes(const struct device *dev); static inline int regulator_count_modes(const struct device *dev)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->count_modes == NULL) {
return 0;
}
return api->count_modes(dev);
}
/** /**
* @brief Return supported voltage * @brief Return supported voltage
@ -121,7 +158,18 @@ int regulator_count_modes(const struct device *dev);
* @param selector: voltage selector code. * @param selector: voltage selector code.
* @return voltage level in uV, or zero if selector code can't be used. * @return voltage level in uV, or zero if selector code can't be used.
*/ */
int regulator_list_voltages(const struct device *dev, unsigned int selector); static inline int regulator_list_voltages(const struct device *dev,
unsigned int selector)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->list_voltages == NULL) {
return 0;
}
return api->list_voltages(dev, selector);
}
/** /**
* @brief Check if a voltage range can be supported. * @brief Check if a voltage range can be supported.
@ -131,7 +179,18 @@ int regulator_list_voltages(const struct device *dev, unsigned int selector);
* @param max_uV: maximum voltage in microvolts * @param max_uV: maximum voltage in microvolts
* @returns boolean or negative error code. * @returns boolean or negative error code.
*/ */
int regulator_is_supported_voltage(const struct device *dev, int min_uV, int max_uV); static inline int regulator_is_supported_voltage(const struct device *dev,
int min_uV, int max_uV)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->is_supported_voltage == NULL) {
return -ENOSYS;
}
return api->is_supported_voltage(dev, min_uV, max_uV);
}
/** /**
* @brief Set regulator output voltage. * @brief Set regulator output voltage.
@ -140,7 +199,18 @@ int regulator_is_supported_voltage(const struct device *dev, int min_uV, int max
* @param min_uV: Minimum acceptable voltage in microvolts * @param min_uV: Minimum acceptable voltage in microvolts
* @param max_uV: Maximum acceptable voltage in microvolts * @param max_uV: Maximum acceptable voltage in microvolts
*/ */
int regulator_set_voltage(const struct device *dev, int min_uV, int max_uV); static inline int regulator_set_voltage(const struct device *dev, int min_uV,
int max_uV)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->set_voltage == NULL) {
return -ENOSYS;
}
return api->set_voltage(dev, min_uV, max_uV);
}
/** /**
* @brief Get regulator output voltage. * @brief Get regulator output voltage.
@ -149,7 +219,17 @@ int regulator_set_voltage(const struct device *dev, int min_uV, int max_uV);
* @param dev: Regulator to query * @param dev: Regulator to query
* @return voltage level in uV * @return voltage level in uV
*/ */
int regulator_get_voltage(const struct device *dev); static inline int regulator_get_voltage(const struct device *dev)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->get_voltage == NULL) {
return 0;
}
return api->get_voltage(dev);
}
/** /**
* @brief Set regulator output current limit * @brief Set regulator output current limit
@ -159,7 +239,18 @@ int regulator_get_voltage(const struct device *dev);
* @param max_uA: maximum microamps * @param max_uA: maximum microamps
* @return 0 on success, or errno on error * @return 0 on success, or errno on error
*/ */
int regulator_set_current_limit(const struct device *dev, int min_uA, int max_uA); static inline int regulator_set_current_limit(const struct device *dev,
int min_uA, int max_uA)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->set_current_limit == NULL) {
return -ENOSYS;
}
return api->set_current_limit(dev, min_uA, max_uA);
}
/** /**
* @brief Get regulator output current. * @brief Get regulator output current.
@ -167,7 +258,17 @@ int regulator_set_current_limit(const struct device *dev, int min_uA, int max_uA
* @param dev: Regulator to query * @param dev: Regulator to query
* @return current limit in uA, or errno * @return current limit in uA, or errno
*/ */
int regulator_get_current_limit(const struct device *dev); static inline int regulator_get_current_limit(const struct device *dev)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->get_current_limit == NULL) {
return -ENOSYS;
}
return api->get_current_limit(dev);
}
/** /**
* @brief Select mode of regulator * @brief Select mode of regulator
@ -179,7 +280,17 @@ int regulator_get_current_limit(const struct device *dev);
* in the regulator-allowed-modes property are permitted. * in the regulator-allowed-modes property are permitted.
* @return 0 on success, or errno on error * @return 0 on success, or errno on error
*/ */
int regulator_set_mode(const struct device *dev, uint32_t mode); static inline int regulator_set_mode(const struct device *dev, uint32_t mode)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->set_mode == NULL) {
return -ENOSYS;
}
return api->set_mode(dev, mode);
}
/** /**
* @brief Set target voltage for regulator mode * @brief Set target voltage for regulator mode
@ -194,8 +305,19 @@ int regulator_set_mode(const struct device *dev, uint32_t mode);
* @param max_uV: maximum voltage acceptable, in uV * @param max_uV: maximum voltage acceptable, in uV
* @return 0 on success, or errno on error * @return 0 on success, or errno on error
*/ */
int regulator_set_mode_voltage(const struct device *dev, uint32_t mode, static inline int regulator_set_mode_voltage(const struct device *dev,
uint32_t min_uV, uint32_t max_uV); uint32_t mode, uint32_t min_uV,
uint32_t max_uV)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->set_mode_voltage == NULL) {
return -ENOSYS;
}
return api->set_mode_voltage(dev, mode, min_uV, max_uV);
}
/** /**
* @brief Get target voltage for regulator mode * @brief Get target voltage for regulator mode
@ -207,7 +329,18 @@ int regulator_set_mode_voltage(const struct device *dev, uint32_t mode,
* @param mode: target mode to query voltage from * @param mode: target mode to query voltage from
* @return voltage level in uV * @return voltage level in uV
*/ */
int regulator_get_mode_voltage(const struct device *dev, uint32_t mode); static inline int regulator_get_mode_voltage(const struct device *dev,
uint32_t mode)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->get_mode_voltage == NULL) {
return 0;
}
return api->get_mode_voltage(dev, mode);
}
/** /**
* @brief Disable regulator for a given mode * @brief Disable regulator for a given mode
@ -217,7 +350,18 @@ int regulator_get_mode_voltage(const struct device *dev, uint32_t mode);
* @param dev: regulator to disable * @param dev: regulator to disable
* @param mode: mode to change regulator state in * @param mode: mode to change regulator state in
*/ */
int regulator_mode_disable(const struct device *dev, uint32_t mode); static inline int regulator_mode_disable(const struct device *dev,
uint32_t mode)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->mode_disable == NULL) {
return -ENOSYS;
}
return api->mode_disable(dev, mode);
}
/** /**
* @brief Enable regulator for a given mode * @brief Enable regulator for a given mode
@ -227,7 +371,18 @@ int regulator_mode_disable(const struct device *dev, uint32_t mode);
* @param dev: regulator to enable * @param dev: regulator to enable
* @param mode: mode to change regulator state in * @param mode: mode to change regulator state in
*/ */
int regulator_mode_enable(const struct device *dev, uint32_t mode); static inline int regulator_mode_enable(const struct device *dev,
uint32_t mode)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
if (api->mode_enable == NULL) {
return -ENOSYS;
}
return api->mode_enable(dev, mode);
}
#ifdef __cplusplus #ifdef __cplusplus
} }