Skip to content

Commit cbd9535

Browse files
bjarki-andreasenkartben
authored andcommitted
drivers: spi: context: Add helper for CS GPIO PM
Introduce spi_context_cs_get() and spi_context_cs_put() which shall be used from drivers to get/put the GPIO port the CS GPIO belongs to before and after a transaction, in line with the SPI drivers pm action hook being called. Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
1 parent c4d475c commit cbd9535

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

drivers/spi/spi_context.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <zephyr/drivers/gpio.h>
1616
#include <zephyr/drivers/spi.h>
1717
#include <zephyr/kernel.h>
18+
#include <zephyr/pm/device_runtime.h>
1819

1920
#ifdef __cplusplus
2021
extern "C" {
@@ -274,6 +275,45 @@ static inline int spi_context_cs_configure_all(struct spi_context *ctx)
274275
return 0;
275276
}
276277

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+
277317
/* Helper function to control the GPIO CS, not meant to be used directly by drivers */
278318
static inline void _spi_context_cs_control(struct spi_context *ctx,
279319
bool on, bool force_off)

0 commit comments

Comments
 (0)