Skip to content

Commit 647055f

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 40be693 commit 647055f

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

drivers/spi/spi_context.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,45 @@ static inline int spi_context_cs_configure_all(struct spi_context *ctx)
274274
return 0;
275275
}
276276

277+
/* Helper function to power manage the GPIO CS pins, not meant to be used directly by drivers */
278+
static inline int _spi_context_cs_pm_all(struct spi_context *ctx, bool get)
279+
{
280+
const struct gpio_dt_spec *cs_gpio;
281+
int ret;
282+
283+
for (cs_gpio = ctx->cs_gpios; cs_gpio < &ctx->cs_gpios[ctx->num_cs_gpios]; cs_gpio++) {
284+
if (get) {
285+
ret = pm_device_runtime_get(cs_gpio->port);
286+
} else {
287+
ret = pm_device_runtime_put(cs_gpio->port);
288+
}
289+
290+
if (ret < 0) {
291+
return ret;
292+
}
293+
}
294+
295+
return 0;
296+
}
297+
298+
/* This function should be called by drivers to pm get all the chip select lines in
299+
* master mode in the case of any CS being a GPIO. This should be called from the
300+
* drivers pm action hook on pm resume.
301+
*/
302+
static inline int spi_context_cs_get_all(struct spi_context *ctx)
303+
{
304+
return _spi_context_cs_pm_all(ctx, true);
305+
}
306+
307+
/* This function should be called by drivers to pm put all the chip select lines in
308+
* master mode in the case of any CS being a GPIO. This should be called from the
309+
* drivers pm action hook on pm suspend.
310+
*/
311+
static inline int spi_context_cs_put_all(struct spi_context *ctx)
312+
{
313+
return _spi_context_cs_pm_all(ctx, false);
314+
}
315+
277316
/* Helper function to control the GPIO CS, not meant to be used directly by drivers */
278317
static inline void _spi_context_cs_control(struct spi_context *ctx,
279318
bool on, bool force_off)

0 commit comments

Comments
 (0)