Skip to content

Commit 906d5ef

Browse files
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 <[email protected]>
1 parent e14f786 commit 906d5ef

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

drivers/spi/spi_context.h

Lines changed: 41 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" {
@@ -233,6 +234,46 @@ static inline int spi_context_cs_configure_all(struct spi_context *ctx)
233234
return 0;
234235
}
235236

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 */
236277
static inline void _spi_context_cs_control(struct spi_context *ctx,
237278
bool on, bool force_off)
238279
{

0 commit comments

Comments
 (0)