drivers: regulator: allow non-thread-safe reference counting

In some cases, it may be desirable to not have thread-safe reference
counting. For example, when CONFIG_MULTITHREADING=n.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2023-04-06 16:26:12 +02:00 committed by Carles Cufí
commit 9faa60aeda
4 changed files with 33 additions and 1 deletions

View file

@ -8,6 +8,13 @@ menuconfig REGULATOR
if REGULATOR if REGULATOR
config REGULATOR_THREAD_SAFE_REFCNT
bool "Thread-safe reference counting"
depends on MULTITHREADING
default y
help
When enabled, regulator reference counting is thread-safe.
config REGULATOR_SHELL config REGULATOR_SHELL
bool "Regulator shell" bool "Regulator shell"
default y default y

View file

@ -9,7 +9,9 @@ void regulator_common_data_init(const struct device *dev)
{ {
struct regulator_common_data *data = dev->data; struct regulator_common_data *data = dev->data;
#ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
(void)k_mutex_init(&data->lock); (void)k_mutex_init(&data->lock);
#endif
data->refcnt = 0; data->refcnt = 0;
} }
@ -88,7 +90,9 @@ int regulator_enable(const struct device *dev)
return 0; return 0;
} }
#ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
(void)k_mutex_lock(&data->lock, K_FOREVER); (void)k_mutex_lock(&data->lock, K_FOREVER);
#endif
data->refcnt++; data->refcnt++;
@ -99,7 +103,9 @@ int regulator_enable(const struct device *dev)
} }
} }
#ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
k_mutex_unlock(&data->lock); k_mutex_unlock(&data->lock);
#endif
return ret; return ret;
} }
@ -113,9 +119,13 @@ bool regulator_is_enabled(const struct device *dev)
if ((config->flags & REGULATOR_ALWAYS_ON) != 0U) { if ((config->flags & REGULATOR_ALWAYS_ON) != 0U) {
enabled = true; enabled = true;
} else { } else {
#ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
(void)k_mutex_lock(&data->lock, K_FOREVER); (void)k_mutex_lock(&data->lock, K_FOREVER);
#endif
enabled = data->refcnt != 0; enabled = data->refcnt != 0;
#ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
k_mutex_unlock(&data->lock); k_mutex_unlock(&data->lock);
#endif
} }
return enabled; return enabled;
@ -138,7 +148,9 @@ int regulator_disable(const struct device *dev)
return 0; return 0;
} }
#ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
(void)k_mutex_lock(&data->lock, K_FOREVER); (void)k_mutex_lock(&data->lock, K_FOREVER);
#endif
data->refcnt--; data->refcnt--;
@ -149,7 +161,9 @@ int regulator_disable(const struct device *dev)
} }
} }
#ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
k_mutex_unlock(&data->lock); k_mutex_unlock(&data->lock);
#endif
return ret; return ret;
} }

View file

@ -20,7 +20,9 @@
#include <zephyr/device.h> #include <zephyr/device.h>
#include <zephyr/devicetree.h> #include <zephyr/devicetree.h>
#ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#endif
#include <zephyr/sys/util_macro.h> #include <zephyr/sys/util_macro.h>
#ifdef __cplusplus #ifdef __cplusplus
@ -182,8 +184,10 @@ struct regulator_common_config {
* This structure **must** be placed first in the driver's data structure. * This structure **must** be placed first in the driver's data structure.
*/ */
struct regulator_common_data { struct regulator_common_data {
/** Lock */ #if defined(CONFIG_REGULATOR_THREAD_SAFE_REFCNT) || defined(__DOXYGEN__)
/** Lock (only if @kconfig{CONFIG_REGULATOR_THREAD_SAFE_REFCNT}=y) */
struct k_mutex lock; struct k_mutex lock;
#endif
/** Reference count */ /** Reference count */
int refcnt; int refcnt;
}; };

View file

@ -7,3 +7,10 @@ tests:
platform_allow: native_posix native_posix_64 platform_allow: native_posix native_posix_64
integration_platforms: integration_platforms:
- native_posix - native_posix
drivers.regulator.api.nothreadsaferefcnt:
tags: drivers regulator
platform_allow: native_posix native_posix_64
integration_platforms:
- native_posix
extra_configs:
- CONFIG_REGULATOR_THREAD_SAFE_REFCNT=n