Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | /*
* Copyright 2019-2020 Peter Bigot Consulting, LLC
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT regulator_fixed
#include <kernel.h>
#include <drivers/regulator.h>
#include <drivers/gpio.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(regulator_fixed, CONFIG_REGULATOR_LOG_LEVEL);
#define OPTION_ALWAYS_ON_POS 0
#define OPTION_ALWAYS_ON BIT(OPTION_ALWAYS_ON_POS)
#define OPTION_BOOT_ON_POS 1
#define OPTION_BOOT_ON BIT(OPTION_BOOT_ON_POS)
struct driver_config {
const char *regulator_name;
const char *gpio_name;
uint32_t startup_delay_us;
uint32_t off_on_delay_us;
gpio_pin_t gpio_pin;
gpio_dt_flags_t gpio_flags;
uint8_t options;
};
enum work_task {
WORK_TASK_UNDEFINED,
WORK_TASK_ENABLE,
WORK_TASK_DISABLE,
WORK_TASK_DELAY,
};
struct driver_data_onoff {
const struct device *gpio;
const struct device *dev;
struct onoff_manager mgr;
#ifdef CONFIG_MULTITHREADING
struct k_work_delayable dwork;
#endif /* CONFIG_MULTITHREADING */
onoff_notify_fn notify;
enum work_task task;
};
/* Common initialization of GPIO device and pin state.
*
* @param dev the regulator device, whether sync or onoff
*
* @param gpiop where to store the GPIO device pointer
*
* @return negative on error, otherwise zero.
*/
static int common_init(const struct device *dev,
const struct device **gpiop)
{
const struct driver_config *cfg = dev->config;
const struct device *gpio = device_get_binding(cfg->gpio_name);
if (gpio == NULL) {
LOG_ERR("no GPIO device: %s", cfg->gpio_name);
return -ENODEV;
}
*gpiop = gpio;
gpio_flags_t flags = cfg->gpio_flags;
bool on = cfg->options & (OPTION_ALWAYS_ON | OPTION_BOOT_ON);
uint32_t delay_us = 0;
if (on) {
flags |= GPIO_OUTPUT_ACTIVE;
delay_us = cfg->startup_delay_us;
} else {
flags |= GPIO_OUTPUT_INACTIVE;
}
int rc = gpio_pin_configure(gpio, cfg->gpio_pin, flags);
if ((rc == 0) && (delay_us > 0)) {
/* Turned on and we have to wait until the on
* completes. Since this is in the driver init we
* can't sleep.
*/
k_busy_wait(delay_us);
}
return rc;
}
static void finalize_transition(struct driver_data_onoff *data,
onoff_notify_fn notify,
uint32_t delay_us,
int rc)
{
const struct driver_config *cfg = data->dev->config;
LOG_DBG("%s: finalize %d delay %u us", cfg->regulator_name, rc, delay_us);
/* If there's no error and we have to delay, do so. */
if ((rc >= 0) && (delay_us > 0)) {
/* If the delay is less than a tick or we're not
* sleep-capable we have to busy-wait.
*/
if ((k_us_to_ticks_floor32(delay_us) == 0)
|| k_is_pre_kernel()
|| !IS_ENABLED(CONFIG_MULTITHREADING)) {
k_busy_wait(delay_us);
#ifdef CONFIG_MULTITHREADING
} else {
/* Otherwise sleep in the work queue. */
__ASSERT_NO_MSG(data->task == WORK_TASK_UNDEFINED);
data->task = WORK_TASK_DELAY;
data->notify = notify;
rc = k_work_schedule(&data->dwork, K_USEC(delay_us));
if (rc >= 0) {
return;
}
#endif /* CONFIG_MULTITHREADING */
}
}
notify(&data->mgr, rc);
}
#ifdef CONFIG_MULTITHREADING
/* The worker is used for several things:
*
* * If a transition occurred in a context where the GPIO state could
* not be changed that's done here.
* * If a start or stop transition requires a delay that exceeds one
* tick the notification after the delay is performed here.
*/
static void onoff_worker(struct k_work *work)
{
struct k_work_delayable *dwork
= k_work_delayable_from_work(work);
struct driver_data_onoff *data
= CONTAINER_OF(dwork, struct driver_data_onoff,
dwork);
onoff_notify_fn notify = data->notify;
const struct driver_config *cfg = data->dev->config;
uint32_t delay_us = 0;
int rc = 0;
if (data->task == WORK_TASK_ENABLE) {
rc = gpio_pin_set(data->gpio, cfg->gpio_pin, true);
LOG_DBG("%s: work enable: %d", cfg->regulator_name, rc);
delay_us = cfg->startup_delay_us;
} else if (data->task == WORK_TASK_DISABLE) {
rc = gpio_pin_set(data->gpio, cfg->gpio_pin, false);
LOG_DBG("%s: work disable: %d", cfg->regulator_name, rc);
delay_us = cfg->off_on_delay_us;
} else if (data->task == WORK_TASK_DELAY) {
LOG_DBG("%s: work delay complete", cfg->regulator_name);
}
data->notify = NULL;
data->task = WORK_TASK_UNDEFINED;
finalize_transition(data, notify, delay_us, rc);
}
#endif /* CONFIG_MULTITHREADING */
static void start(struct onoff_manager *mgr,
onoff_notify_fn notify)
{
struct driver_data_onoff *data =
CONTAINER_OF(mgr, struct driver_data_onoff, mgr);
const struct driver_config *cfg = data->dev->config;
uint32_t delay_us = cfg->startup_delay_us;
int rc = 0;
LOG_DBG("%s: start", cfg->regulator_name);
if ((cfg->options & OPTION_ALWAYS_ON) != 0) {
delay_us = 0;
goto finalize;
}
rc = gpio_pin_set(data->gpio, cfg->gpio_pin, true);
#ifdef CONFIG_MULTITHREADING
if (rc == -EWOULDBLOCK) {
/* Perform the enable and finalization in a work item.
*/
LOG_DBG("%s: start deferred", cfg->regulator_name);
__ASSERT_NO_MSG(data->task == WORK_TASK_UNDEFINED);
data->task = WORK_TASK_ENABLE;
data->notify = notify;
k_work_schedule(&data->dwork, K_NO_WAIT);
return;
}
#endif /* CONFIG_MULTITHREADING */
finalize:
finalize_transition(data, notify, delay_us, rc);
return;
}
static void stop(struct onoff_manager *mgr,
onoff_notify_fn notify)
{
struct driver_data_onoff *data =
CONTAINER_OF(mgr, struct driver_data_onoff, mgr);
const struct driver_config *cfg = data->dev->config;
uint32_t delay_us = cfg->off_on_delay_us;
int rc = 0;
LOG_DBG("%s: stop", cfg->regulator_name);
if ((cfg->options & OPTION_ALWAYS_ON) != 0) {
delay_us = 0;
goto finalize;
}
rc = gpio_pin_set(data->gpio, cfg->gpio_pin, false);
#ifdef CONFIG_MULTITHREADING
if (rc == -EWOULDBLOCK) {
/* Perform the disable and finalization in a work
* item.
*/
LOG_DBG("%s: stop deferred", cfg->regulator_name);
__ASSERT_NO_MSG(data->task == WORK_TASK_UNDEFINED);
data->task = WORK_TASK_DISABLE;
data->notify = notify;
k_work_schedule(&data->dwork, K_NO_WAIT);
return;
}
#endif /* CONFIG_MULTITHREADING */
finalize:
finalize_transition(data, notify, delay_us, rc);
return;
}
static int enable_onoff(const struct device *dev, struct onoff_client *cli)
{
struct driver_data_onoff *data = dev->data;
return onoff_request(&data->mgr, cli);
}
static int disable_onoff(const struct device *dev)
{
struct driver_data_onoff *data = dev->data;
return onoff_release(&data->mgr);
}
static const struct onoff_transitions transitions =
ONOFF_TRANSITIONS_INITIALIZER(start, stop, NULL);
static const struct regulator_driver_api api_onoff = {
.enable = enable_onoff,
.disable = disable_onoff,
};
static int regulator_fixed_init_onoff(const struct device *dev)
{
struct driver_data_onoff *data = dev->data;
int rc;
data->dev = dev;
rc = onoff_manager_init(&data->mgr, &transitions);
__ASSERT_NO_MSG(rc == 0);
#ifdef CONFIG_MULTITHREADING
k_work_init_delayable(&data->dwork, onoff_worker);
#endif /* CONFIG_MULTITHREADING */
rc = common_init(dev, &data->gpio);
if (rc >= 0) {
rc = 0;
}
LOG_INF("%s onoff: %d", dev->name, rc);
return rc;
}
struct driver_data_sync {
const struct device *gpio;
struct onoff_sync_service srv;
};
#if DT_HAS_COMPAT_STATUS_OKAY(regulator_fixed_sync) - 0
static int enable_sync(const struct device *dev, struct onoff_client *cli)
{
struct driver_data_sync *data = dev->data;
const struct driver_config *cfg = dev->config;
k_spinlock_key_t key;
int rc = onoff_sync_lock(&data->srv, &key);
if ((rc == 0)
&& ((cfg->options & OPTION_ALWAYS_ON) == 0)) {
rc = gpio_pin_set(data->gpio, cfg->gpio_pin, true);
}
return onoff_sync_finalize(&data->srv, key, cli, rc, true);
}
static int disable_sync(const struct device *dev)
{
struct driver_data_sync *data = dev->data;
const struct driver_config *cfg = dev->config;
k_spinlock_key_t key;
int rc = onoff_sync_lock(&data->srv, &key);
if ((cfg->options & OPTION_ALWAYS_ON) != 0) {
rc = 0;
} else if (rc == 1) {
rc = gpio_pin_set(data->gpio, cfg->gpio_pin, false);
} else if (rc == 0) {
rc = -EINVAL;
} /* else rc > 0, leave it on */
return onoff_sync_finalize(&data->srv, key, NULL, rc, false);
}
static const struct regulator_driver_api api_sync = {
.enable = enable_sync,
.disable = disable_sync,
};
static int regulator_fixed_init_sync(const struct device *dev)
{
struct driver_data_sync *data = dev->data;
const struct driver_config *cfg = dev->config;
int rc = common_init(dev, &data->gpio);
(void)regulator_fixed_init_onoff;
(void)api_onoff;
(void)cfg;
__ASSERT(cfg->startup_delay_us == 0,
"sync not valid with startup delay");
__ASSERT(cfg->off_on_delay_us == 0,
"sync not valid with shutdown delay");
LOG_INF("%s sync: %d", dev->name, rc);
return rc;
}
#endif /* DT_HAS_COMPAT_STATUS_OK(regulator_fixed_sync) */
/* This should also check:
* && DT_INST_PROP(id, startup_delay_us) == 0
* && DT_INST_PROP(id, off_on_delay_us) == 0
* but the preprocessor magic doesn't seem able to do that so we'll assert
* in init instead.
*/
#define REG_IS_SYNC(id) \
DT_NODE_HAS_COMPAT(DT_DRV_INST(id), regulator_fixed_sync)
#define REG_DATA_TAG(id) COND_CODE_1(REG_IS_SYNC(id), \
(driver_data_sync), \
(driver_data_onoff))
#define REG_API(id) COND_CODE_1(REG_IS_SYNC(id), \
(api_sync), \
(api_onoff))
#define REG_INIT(id) COND_CODE_1(REG_IS_SYNC(id), \
(regulator_fixed_init_sync), \
(regulator_fixed_init_onoff))
#define REGULATOR_DEVICE(id) \
static const struct driver_config regulator_##id##_cfg = { \
.regulator_name = DT_INST_PROP(id, regulator_name), \
.gpio_name = DT_INST_GPIO_LABEL(id, enable_gpios), \
.startup_delay_us = DT_INST_PROP(id, startup_delay_us), \
.off_on_delay_us = DT_INST_PROP(id, off_on_delay_us), \
.gpio_pin = DT_INST_GPIO_PIN(id, enable_gpios), \
.gpio_flags = DT_INST_GPIO_FLAGS(id, enable_gpios), \
.options = (DT_INST_PROP(id, regulator_boot_on) \
<< OPTION_BOOT_ON_POS) \
| (DT_INST_PROP(id, regulator_always_on) \
<< OPTION_ALWAYS_ON_POS), \
}; \
\
static struct REG_DATA_TAG(id) regulator_##id##_data; \
\
DEVICE_DT_INST_DEFINE(id, REG_INIT(id), NULL, \
®ulator_##id##_data, ®ulator_##id##_cfg, \
POST_KERNEL, CONFIG_REGULATOR_FIXED_INIT_PRIORITY, \
®_API(id));
DT_INST_FOREACH_STATUS_OKAY(REGULATOR_DEVICE)
|