Drop the async enable function. This feature is rarely/never used, complicates driver design, and doesn't really follow the sync/async API design/naming used in other areas. In the future we can introduce regulator_enable_async if needed, with support from the driver class (no onoff). Note that drivers like PCA9420 did not implement any asynchronous behavior. regulator-fixed implemented in the past asynchronous behavior using work queues, an overkill for most GPIO driven regulators. Let's keep things simple for now and extend the API when needed, based on specific usecases. In the current implementation, reference counting is managed by the driver class. \isr-ok attribute is dropped, since calls are potentially blocking. Note that drivers like PCA9420 already violated such rule. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
66 lines
1.2 KiB
C
66 lines
1.2 KiB
C
/*
|
|
* Copyright 2022 Nordic Semiconductor ASA
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <zephyr/drivers/regulator.h>
|
|
|
|
void regulator_common_data_init(const struct device *dev)
|
|
{
|
|
struct regulator_common_data *data =
|
|
(struct regulator_common_data *)dev->data;
|
|
|
|
(void)k_mutex_init(&data->lock);
|
|
data->refcnt = 0;
|
|
}
|
|
|
|
int regulator_enable(const struct device *dev)
|
|
{
|
|
struct regulator_common_data *data =
|
|
(struct regulator_common_data *)dev->data;
|
|
int ret = 0;
|
|
|
|
(void)k_mutex_lock(&data->lock, K_FOREVER);
|
|
|
|
data->refcnt++;
|
|
|
|
if (data->refcnt == 1) {
|
|
const struct regulator_driver_api *api =
|
|
(const struct regulator_driver_api *)dev->api;
|
|
|
|
ret = api->enable(dev);
|
|
if (ret < 0) {
|
|
data->refcnt--;
|
|
}
|
|
}
|
|
|
|
k_mutex_unlock(&data->lock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int regulator_disable(const struct device *dev)
|
|
{
|
|
struct regulator_common_data *data =
|
|
(struct regulator_common_data *)dev->data;
|
|
int ret = 0;
|
|
|
|
(void)k_mutex_lock(&data->lock, K_FOREVER);
|
|
|
|
data->refcnt--;
|
|
|
|
if (data->refcnt == 0) {
|
|
const struct regulator_driver_api *api =
|
|
(const struct regulator_driver_api *)dev->api;
|
|
|
|
ret = api->disable(dev);
|
|
if (ret < 0) {
|
|
data->refcnt++;
|
|
}
|
|
}
|
|
|
|
k_mutex_unlock(&data->lock);
|
|
|
|
return ret;
|
|
}
|