Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gpiod_* vs gpio_* methods in the Linux Kernel

Which of the GPIO APIs in Linux device driver programming is preferable and in what conditions: gpio_set_value() or gpiod_set_value()?

One takes the GPIO desc as the object while other takes the GPIO parsed from the device tree.

like image 303
Raulp Avatar asked Dec 24 '22 02:12

Raulp


1 Answers

For all new drivers it's recommended to use gpiod_* API. Old gpio_* API is considered deprecated now.

From this commit:

gpiolib: export descriptor-based GPIO interface

This patch exports the gpiod_* family of API functions, a safer alternative to the legacy GPIO interface. Differences between the gpiod and legacy gpio APIs are:

  • gpio works with integers, whereas gpiod operates on opaque handlers which cannot be forged or used before proper acquisition
  • gpiod get/set functions are aware of the active low state of a GPIO
  • gpio consumers should now include <linux/gpio/consumer.h> to access the new interface, whereas chips drivers will use <linux/gpio/driver.h>

The legacy gpio API is now built as inline functions on top of gpiod.

See next links for details on new gpiod API:

  • [LWN article] GPIO in the kernel: future directions
  • [Kernel documentation] GPIO Descriptor Consumer Interface

    (and rest of gpiod kernel documentation added by this commit)

like image 66
Sam Protsenko Avatar answered Jan 03 '23 04:01

Sam Protsenko