|
15 | 15 | #include <zephyr/drivers/gpio.h>
|
16 | 16 | #include <zephyr/drivers/spi.h>
|
17 | 17 | #include <zephyr/kernel.h>
|
| 18 | +#include <zephyr/pm/device_runtime.h> |
18 | 19 |
|
19 | 20 | #ifdef __cplusplus
|
20 | 21 | extern "C" {
|
@@ -274,6 +275,45 @@ static inline int spi_context_cs_configure_all(struct spi_context *ctx)
|
274 | 275 | return 0;
|
275 | 276 | }
|
276 | 277 |
|
| 278 | +/* Helper function to power manage the GPIO CS pins, not meant to be used directly by drivers */ |
| 279 | +static inline int _spi_context_cs_pm_all(struct spi_context *ctx, bool get) |
| 280 | +{ |
| 281 | + const struct gpio_dt_spec *cs_gpio; |
| 282 | + int ret; |
| 283 | + |
| 284 | + for (cs_gpio = ctx->cs_gpios; cs_gpio < &ctx->cs_gpios[ctx->num_cs_gpios]; cs_gpio++) { |
| 285 | + if (get) { |
| 286 | + ret = pm_device_runtime_get(cs_gpio->port); |
| 287 | + } else { |
| 288 | + ret = pm_device_runtime_put(cs_gpio->port); |
| 289 | + } |
| 290 | + |
| 291 | + if (ret < 0) { |
| 292 | + return ret; |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + return 0; |
| 297 | +} |
| 298 | + |
| 299 | +/* This function should be called by drivers to pm get all the chip select lines in |
| 300 | + * master mode in the case of any CS being a GPIO. This should be called from the |
| 301 | + * drivers pm action hook on pm resume. |
| 302 | + */ |
| 303 | +static inline int spi_context_cs_get_all(struct spi_context *ctx) |
| 304 | +{ |
| 305 | + return _spi_context_cs_pm_all(ctx, true); |
| 306 | +} |
| 307 | + |
| 308 | +/* This function should be called by drivers to pm put all the chip select lines in |
| 309 | + * master mode in the case of any CS being a GPIO. This should be called from the |
| 310 | + * drivers pm action hook on pm suspend. |
| 311 | + */ |
| 312 | +static inline int spi_context_cs_put_all(struct spi_context *ctx) |
| 313 | +{ |
| 314 | + return _spi_context_cs_pm_all(ctx, false); |
| 315 | +} |
| 316 | + |
277 | 317 | /* Helper function to control the GPIO CS, not meant to be used directly by drivers */
|
278 | 318 | static inline void _spi_context_cs_control(struct spi_context *ctx,
|
279 | 319 | bool on, bool force_off)
|
|
0 commit comments