|
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" { |
@@ -233,6 +234,46 @@ static inline int spi_context_cs_configure_all(struct spi_context *ctx) |
233 | 234 | return 0; |
234 | 235 | } |
235 | 236 |
|
| 237 | +/* Helper function to power manage the GPIO CS pins, not meant to be used directly by drivers */ |
| 238 | +static inline int _spi_context_cs_pm_all(struct spi_context *ctx, bool get) |
| 239 | +{ |
| 240 | + const struct gpio_dt_spec *cs_gpio; |
| 241 | + int ret; |
| 242 | + |
| 243 | + for (cs_gpio = ctx->cs_gpios; cs_gpio < &ctx->cs_gpios[ctx->num_cs_gpios]; cs_gpio++) { |
| 244 | + if (get) { |
| 245 | + ret = pm_device_runtime_get(cs_gpio->port); |
| 246 | + } else { |
| 247 | + ret = pm_device_runtime_put(cs_gpio->port); |
| 248 | + } |
| 249 | + |
| 250 | + if (ret < 0) { |
| 251 | + return ret; |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + return 0; |
| 256 | +} |
| 257 | + |
| 258 | +/* This function should be called by drivers to pm get all the chip select lines in |
| 259 | + * master mode in the case of any CS being a GPIO. This should be called from the |
| 260 | + * drivers pm action hook on pm resume. |
| 261 | + */ |
| 262 | +static inline int spi_context_cs_get_all(struct spi_context *ctx) |
| 263 | +{ |
| 264 | + return _spi_context_cs_pm_all(ctx, true); |
| 265 | +} |
| 266 | + |
| 267 | +/* This function should be called by drivers to pm put all the chip select lines in |
| 268 | + * master mode in the case of any CS being a GPIO. This should be called from the |
| 269 | + * drivers pm action hook on pm suspend. |
| 270 | + */ |
| 271 | +static inline int spi_context_cs_put_all(struct spi_context *ctx) |
| 272 | +{ |
| 273 | + return _spi_context_cs_pm_all(ctx, false); |
| 274 | +} |
| 275 | + |
| 276 | +/* Helper function to control the GPIO CS, not meant to be used directly by drivers */ |
236 | 277 | static inline void _spi_context_cs_control(struct spi_context *ctx, |
237 | 278 | bool on, bool force_off) |
238 | 279 | { |
|
0 commit comments