blob: 781a053abbb99a1d3c048f3c50879dcec3e366e9 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __LINUX_GPIO_MACHINE_H
3#define __LINUX_GPIO_MACHINE_H
4
5#include <linux/types.h>
6#include <linux/list.h>
7
8enum gpio_lookup_flags {
David Brazdil0f672f62019-12-10 10:32:29 +00009 GPIO_ACTIVE_HIGH = (0 << 0),
10 GPIO_ACTIVE_LOW = (1 << 0),
11 GPIO_OPEN_DRAIN = (1 << 1),
12 GPIO_OPEN_SOURCE = (1 << 2),
13 GPIO_PERSISTENT = (0 << 3),
14 GPIO_TRANSITORY = (1 << 3),
15 GPIO_PULL_UP = (1 << 4),
16 GPIO_PULL_DOWN = (1 << 5),
17
18 GPIO_LOOKUP_FLAGS_DEFAULT = GPIO_ACTIVE_HIGH | GPIO_PERSISTENT,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019};
20
21/**
22 * struct gpiod_lookup - lookup table
Olivier Deprez157378f2022-04-04 15:47:50 +020023 * @key: either the name of the chip the GPIO belongs to, or the GPIO line name
24 * Note that GPIO line names are not guaranteed to be globally unique,
25 * so this will use the first match found!
26 * @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO, or
27 * U16_MAX to indicate that @key is a GPIO line name
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028 * @con_id: name of the GPIO from the device's point of view
29 * @idx: index of the GPIO in case several GPIOs share the same name
David Brazdil0f672f62019-12-10 10:32:29 +000030 * @flags: bitmask of gpio_lookup_flags GPIO_* values
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031 *
32 * gpiod_lookup is a lookup table for associating GPIOs to specific devices and
33 * functions using platform data.
34 */
35struct gpiod_lookup {
Olivier Deprez157378f2022-04-04 15:47:50 +020036 const char *key;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037 u16 chip_hwnum;
38 const char *con_id;
39 unsigned int idx;
David Brazdil0f672f62019-12-10 10:32:29 +000040 unsigned long flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041};
42
43struct gpiod_lookup_table {
44 struct list_head list;
45 const char *dev_id;
46 struct gpiod_lookup table[];
47};
48
49/**
50 * struct gpiod_hog - GPIO line hog table
51 * @chip_label: name of the chip the GPIO belongs to
52 * @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO
53 * @line_name: consumer name for the hogged line
David Brazdil0f672f62019-12-10 10:32:29 +000054 * @lflags: bitmask of gpio_lookup_flags GPIO_* values
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 * @dflags: GPIO flags used to specify the direction and value
56 */
57struct gpiod_hog {
58 struct list_head list;
59 const char *chip_label;
60 u16 chip_hwnum;
61 const char *line_name;
David Brazdil0f672f62019-12-10 10:32:29 +000062 unsigned long lflags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063 int dflags;
64};
65
66/*
67 * Simple definition of a single GPIO under a con_id
68 */
Olivier Deprez157378f2022-04-04 15:47:50 +020069#define GPIO_LOOKUP(_key, _chip_hwnum, _con_id, _flags) \
70 GPIO_LOOKUP_IDX(_key, _chip_hwnum, _con_id, 0, _flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071
72/*
73 * Use this macro if you need to have several GPIOs under the same con_id.
74 * Each GPIO needs to use a different index and can be accessed using
75 * gpiod_get_index()
76 */
Olivier Deprez157378f2022-04-04 15:47:50 +020077#define GPIO_LOOKUP_IDX(_key, _chip_hwnum, _con_id, _idx, _flags) \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078{ \
Olivier Deprez157378f2022-04-04 15:47:50 +020079 .key = _key, \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080 .chip_hwnum = _chip_hwnum, \
81 .con_id = _con_id, \
82 .idx = _idx, \
83 .flags = _flags, \
84}
85
86/*
87 * Simple definition of a single GPIO hog in an array.
88 */
89#define GPIO_HOG(_chip_label, _chip_hwnum, _line_name, _lflags, _dflags) \
90{ \
91 .chip_label = _chip_label, \
92 .chip_hwnum = _chip_hwnum, \
93 .line_name = _line_name, \
94 .lflags = _lflags, \
95 .dflags = _dflags, \
96}
97
98#ifdef CONFIG_GPIOLIB
99void gpiod_add_lookup_table(struct gpiod_lookup_table *table);
100void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n);
101void gpiod_remove_lookup_table(struct gpiod_lookup_table *table);
102void gpiod_add_hogs(struct gpiod_hog *hogs);
David Brazdil0f672f62019-12-10 10:32:29 +0000103#else /* ! CONFIG_GPIOLIB */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104static inline
105void gpiod_add_lookup_table(struct gpiod_lookup_table *table) {}
106static inline
107void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) {}
108static inline
109void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) {}
110static inline void gpiod_add_hogs(struct gpiod_hog *hogs) {}
David Brazdil0f672f62019-12-10 10:32:29 +0000111#endif /* CONFIG_GPIOLIB */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112
113#endif /* __LINUX_GPIO_MACHINE_H */