Skip to content

Commit

Permalink
drivers: regulator: use int32_t for voltages/currents
Browse files Browse the repository at this point in the history
Right now we had a mix of int (signed) and uint32_t (fixed-width
unsigned integer). Use int32_t for all cases, both values may be either
positive/negative, and we have a defined range when using fixed-width
integers.

Signed-off-by: Gerard Marull-Paretas <[email protected]>
  • Loading branch information
gmarull authored and carlescufi committed Nov 14, 2022
1 parent 561eb2b commit 6d744d3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
38 changes: 21 additions & 17 deletions drivers/regulator/regulator_pca9420.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ struct regulator_config {
int num_modes;
uint8_t vsel_reg;
uint8_t vsel_mask;
uint32_t max_uV;
uint32_t min_uV;
int32_t max_uV;
int32_t min_uV;
uint8_t enable_reg;
uint8_t enable_mask;
uint8_t enable_val;
Expand All @@ -56,7 +56,8 @@ struct regulator_config {
};

static int regulator_pca9420_is_supported_voltage(const struct device *dev,
int min_uV, int max_uV);
int32_t min_uV,
int32_t max_uV);

/**
* Reads a register from the PMIC
Expand Down Expand Up @@ -101,7 +102,8 @@ static int regulator_modify_register(const struct device *dev,
* offset applied to the vsel_reg. Useful to support reading voltages
* in another target mode
*/
static int regulator_get_voltage_offset(const struct device *dev, uint32_t off)
static int32_t regulator_get_voltage_offset(const struct device *dev,
uint32_t off)
{
const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data;
Expand Down Expand Up @@ -130,8 +132,9 @@ static int regulator_get_voltage_offset(const struct device *dev, uint32_t off)
* offset applied to the vsel_reg. Useful to support setting voltages in
* another target mode.
*/
static int regulator_set_voltage_offset(const struct device *dev, int min_uV,
int max_uV, uint32_t off)
static int regulator_set_voltage_offset(const struct device *dev,
int32_t min_uV, int32_t max_uV,
uint32_t off)
{
const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data;
Expand Down Expand Up @@ -187,8 +190,8 @@ static int regulator_pca9420_count_modes(const struct device *dev)
* Part of the extended regulator consumer API
* Returns the supported voltage in uV for a given selector value
*/
static int regulator_pca9420_list_voltages(const struct device *dev,
unsigned int selector)
static int32_t regulator_pca9420_list_voltages(const struct device *dev,
unsigned int selector)
{
const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data;
Expand All @@ -204,7 +207,8 @@ static int regulator_pca9420_list_voltages(const struct device *dev,
* Returns true if the regulator supports a voltage in the given range.
*/
static int regulator_pca9420_is_supported_voltage(const struct device *dev,
int min_uV, int max_uV)
int32_t min_uV,
int32_t max_uV)
{
const struct regulator_config *config = dev->config;

Expand All @@ -215,8 +219,8 @@ static int regulator_pca9420_is_supported_voltage(const struct device *dev,
* Part of the extended regulator consumer API
* Sets the output voltage to the closest supported voltage value
*/
static int regulator_pca9420_set_voltage(const struct device *dev, int min_uV,
int max_uV)
static int regulator_pca9420_set_voltage(const struct device *dev,
int32_t min_uV, int32_t max_uV)
{
return regulator_set_voltage_offset(dev, min_uV, max_uV, 0);
}
Expand All @@ -226,7 +230,7 @@ static int regulator_pca9420_set_voltage(const struct device *dev, int min_uV,
* Part of the extended regulator consumer API
* Gets the current output voltage in uV
*/
static int regulator_pca9420_get_voltage(const struct device *dev)
static int32_t regulator_pca9420_get_voltage(const struct device *dev)
{
return regulator_get_voltage_offset(dev, 0);
}
Expand All @@ -236,7 +240,7 @@ static int regulator_pca9420_get_voltage(const struct device *dev)
* Set the current limit for this device
*/
static int regulator_pca9420_set_current_limit(const struct device *dev,
int min_uA, int max_uA)
int32_t min_uA, int32_t max_uA)
{
const struct regulator_config *config = dev->config;
struct regulator_data *data = dev->data;
Expand Down Expand Up @@ -297,8 +301,8 @@ static int regulator_pca9420_get_current_limit(const struct device *dev)
* with the regulator_pca9420_set_mode api
*/
static int regulator_pca9420_set_mode_voltage(const struct device *dev,
uint32_t mode, uint32_t min_uV,
uint32_t max_uV)
uint32_t mode, int32_t min_uV,
int32_t max_uV)
{
const struct regulator_config *config = dev->config;
uint8_t i, sel_off;
Expand Down Expand Up @@ -389,8 +393,8 @@ static int regulator_pca9420_mode_enable(const struct device *dev,
* not need to be the active mode. This API can be used to read voltages
* from a regulator mode other than the default.
*/
static int regulator_pca9420_get_mode_voltage(const struct device *dev,
uint32_t mode)
static int32_t regulator_pca9420_get_mode_voltage(const struct device *dev,
uint32_t mode)
{
const struct regulator_config *config = dev->config;
uint8_t i, sel_off;
Expand Down
44 changes: 23 additions & 21 deletions include/zephyr/drivers/regulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ __subsystem struct regulator_driver_api {
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);
int32_t (*list_voltages)(const struct device *dev,
unsigned int selector);
int (*is_supported_voltage)(const struct device *dev, int32_t min_uV,
int32_t max_uV);
int (*set_voltage)(const struct device *dev, int32_t min_uV,
int32_t max_uV);
int32_t (*get_voltage)(const struct device *dev);
int (*set_current_limit)(const struct device *dev, int32_t min_uA,
int32_t 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);
int32_t min_uV, int32_t max_uV);
int32_t (*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);
};
Expand Down Expand Up @@ -161,8 +163,8 @@ static inline int regulator_count_modes(const struct device *dev)
* @return voltage Voltage level in microvolts.
* @retval 0 If selector code can't be used.
*/
static inline int regulator_list_voltages(const struct device *dev,
unsigned int selector)
static inline int32_t regulator_list_voltages(const struct device *dev,
unsigned int selector)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
Expand All @@ -186,7 +188,7 @@ static inline int regulator_list_voltages(const struct device *dev,
* @retval -errno In case of any other error.
*/
static inline int regulator_is_supported_voltage(const struct device *dev,
int min_uV, int max_uV)
int32_t min_uV, int32_t max_uV)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
Expand All @@ -213,8 +215,8 @@ static inline int regulator_is_supported_voltage(const struct device *dev,
* @retval -ENOSYS If function is not implemented.
* @retval -errno In case of any other error.
*/
static inline int regulator_set_voltage(const struct device *dev, int min_uV,
int max_uV)
static inline int regulator_set_voltage(const struct device *dev,
int32_t min_uV, int32_t max_uV)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
Expand All @@ -233,7 +235,7 @@ static inline int regulator_set_voltage(const struct device *dev, int min_uV,
*
* @return Voltage level in microvolts.
*/
static inline int regulator_get_voltage(const struct device *dev)
static inline int32_t regulator_get_voltage(const struct device *dev)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
Expand All @@ -257,7 +259,7 @@ static inline int regulator_get_voltage(const struct device *dev)
* @retval -errno In case of any other error.
*/
static inline int regulator_set_current_limit(const struct device *dev,
int min_uA, int max_uA)
int32_t min_uA, int32_t max_uA)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
Expand All @@ -278,7 +280,7 @@ static inline int regulator_set_current_limit(const struct device *dev,
* @retval -ENOSYS If function is not implemented.
* @retval -errno In case of any other error.
*/
static inline int regulator_get_current_limit(const struct device *dev)
static inline int32_t regulator_get_current_limit(const struct device *dev)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
Expand Down Expand Up @@ -333,8 +335,8 @@ static inline int regulator_set_mode(const struct device *dev, uint32_t mode)
* @retval -errno In case of any other error.
*/
static inline int regulator_set_mode_voltage(const struct device *dev,
uint32_t mode, uint32_t min_uV,
uint32_t max_uV)
uint32_t mode, int32_t min_uV,
int32_t max_uV)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
Expand All @@ -357,8 +359,8 @@ static inline int regulator_set_mode_voltage(const struct device *dev,
*
* @return Voltage level in microvolts.
*/
static inline int regulator_get_mode_voltage(const struct device *dev,
uint32_t mode)
static inline int32_t regulator_get_mode_voltage(const struct device *dev,
uint32_t mode)
{
const struct regulator_driver_api *api =
(const struct regulator_driver_api *)dev->api;
Expand Down

0 comments on commit 6d744d3

Please sign in to comment.