GPIO controller driver struct gpio_chip

Posted by pvechi on Tue, 04 Jan 2022 05:02:05 +0100

gpio_chip

This chapter explains how to write drivers for these devices step by step, so it includes the following topics:
GPIO controller driver structure and data structure
Sysfs interface of GPIO controller
Representation of GPIO controller in DT
Driver architecture and data structure
Drivers for such devices shall provide the following:

Method for establishing GPIO direction (input and output).
Methods (get and set) for accessing GPIO values.
A method that maps a given GPIO to an IRQ and returns the relevant number.
A flag indicating whether calls to its methods can sleep. This is very important.
An optional debugfs dump method (showing additional status, such as pullup config).
An optional number called base number from which the GPIO number should start. If omitted, it will be automatically assigned.
In the kernel, the GPIO controller is represented as Linux / GPIO / driver Structure GPIO defined in H_ Instance of chip:
gpio_chip structure

struct gpio_chip {
  const char *label;
  struct device *dev;
  struct module *owner;
  int (*request)(struct gpio_chip *chip, unsigned offset);
  void (*free)(struct gpio_chip *chip, unsigned offset);
  int (*get_direction)(struct gpio_chip *chip, unsigned offset);
  int (*direction_input)(struct gpio_chip *chip, unsigned offset);
  int (*direction_output)(struct gpio_chip *chip, unsigned offset,
  int value);
  int (*get)(struct gpio_chip *chip,unsigned offset);
  void (*set)(struct gpio_chip *chip, unsigned offset, int value);
  void (*set_multiple)(struct gpio_chip *chip, unsigned long *mask,
  unsigned long *bits);
  int (*set_debounce)(struct gpio_chip *chip, unsigned offset,
  unsigned debounce);
  int (*to_irq)(struct gpio_chip *chip, unsigned offset);
  int base;
  u16 ngpio;
  const char *const *names;
  bool can_sleep;
  bool irq_not_threaded;
  bool exported;
#ifdef CONFIG_GPIOLIB_IRQCHIP
  /*
   * With CONFIG_GPIOLIB_IRQCHIP we get an irqchip
   * inside the gpiolib to handle IRQs for most practical cases.
   */
  struct irq_chip *irqchip;
  struct irq_domain *irqdomain;
  unsigned int irq_base;
  irq_flow_handler_t irq_handler;
  unsigned int irq_default_type;
#endif
#if defined(CONFIG_OF_GPIO)
  /*
   * If CONFIG_OF is enabled, then all GPIO controllers described in the
    * device tree automatically may have an OF translation
   */
  struct device_node *of_node;
  int of_gpio_n_cells;
  int (*of_xlate)(struct gpio_chip *gc,
  const struct of_phandle_args *gpiospec, u32 *flags);
};

The following is the meaning of each element in the structure:

request is an optional callback function activated by a specific chip. If so, call gpio_request() or gpiod_ When get(), it executes before allocating GPIO.
Free is an optional callback function for chip specific release. If so, call gpiod_put() or GPIO_ When free(), it executes before GPIO is released.
get_direction executes the GPIO offset when you need to know the direction. The return value should be 0 for out, 1 for in (the same as GPIOF_DIR_XXX), or negative error.
direction_input configures the signal offset offset as input, otherwise an error is returned.
get returns the value of GPIO offset; For the output signal, this will return the actual perceived value or 0.
set specifies an output value to GPIO offset.
When it is necessary to assign output values to multiple signals defined by the mask, call set_multiple. If not provided, the kernel will install a general callback function that will traverse the mask bits and execute chip - > set (I) on each bit.
Look at the following code, which shows how to implement this function:

reference

GPIO controller driver - gpio_chip

Topics: data structure