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
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
bool "Regulator shell"
default y

View file

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