Skip to content

Commit a411e81

Browse files
brglLinus Walleij
authored andcommitted
gpiolib: add hogs support for machine code
Board files constitute a significant part of the users of the legacy GPIO framework. In many cases they only export a line and set its desired value. We could use GPIO hogs for that like we do for DT and ACPI but there's no support for that in machine code. This patch proposes to extend the machine.h API with support for registering hog tables in board files. Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 192a35b commit a411e81

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

‎Documentation/driver-api/gpio/board.rst‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,19 @@ mapping and is thus transparent to GPIO consumers.
177177

178178
A set of functions such as gpiod_set_value() is available to work with
179179
the new descriptor-oriented interface.
180+
181+
Boards using platform data can also hog GPIO lines by defining GPIO hog tables.
182+
183+
.. code-block:: c
184+
185+
struct gpiod_hog gpio_hog_table[] = {
186+
GPIO_HOG("gpio.0", 10, "foo", GPIO_ACTIVE_LOW, GPIOD_OUT_HIGH),
187+
{ }
188+
};
189+
190+
And the table can be added to the board code as follows::
191+
192+
gpiod_add_hogs(gpio_hog_table);
193+
194+
The line will be hogged as soon as the gpiochip is created or - in case the
195+
chip was created earlier - when the hog table is registered.

‎drivers/gpio/gpiolib.c‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ static DEFINE_MUTEX(gpio_lookup_lock);
7171
static LIST_HEAD(gpio_lookup_list);
7272
LIST_HEAD(gpio_devices);
7373

74+
static DEFINE_MUTEX(gpio_machine_hogs_mutex);
75+
static LIST_HEAD(gpio_machine_hogs);
76+
7477
static void gpiochip_free_hogs(struct gpio_chip *chip);
7578
static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
7679
struct lock_class_key *lock_key,
@@ -1171,6 +1174,41 @@ static int gpiochip_setup_dev(struct gpio_device *gdev)
11711174
return status;
11721175
}
11731176

1177+
static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
1178+
{
1179+
struct gpio_desc *desc;
1180+
int rv;
1181+
1182+
desc = gpiochip_get_desc(chip, hog->chip_hwnum);
1183+
if (IS_ERR(desc)) {
1184+
pr_err("%s: unable to get GPIO desc: %ld\n",
1185+
__func__, PTR_ERR(desc));
1186+
return;
1187+
}
1188+
1189+
if (desc->flags & FLAG_IS_HOGGED)
1190+
return;
1191+
1192+
rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
1193+
if (rv)
1194+
pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
1195+
__func__, chip->label, hog->chip_hwnum, rv);
1196+
}
1197+
1198+
static void machine_gpiochip_add(struct gpio_chip *chip)
1199+
{
1200+
struct gpiod_hog *hog;
1201+
1202+
mutex_lock(&gpio_machine_hogs_mutex);
1203+
1204+
list_for_each_entry(hog, &gpio_machine_hogs, list) {
1205+
if (!strcmp(chip->label, hog->chip_label))
1206+
gpiochip_machine_hog(chip, hog);
1207+
}
1208+
1209+
mutex_unlock(&gpio_machine_hogs_mutex);
1210+
}
1211+
11741212
static void gpiochip_setup_devs(void)
11751213
{
11761214
struct gpio_device *gdev;
@@ -1326,6 +1364,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
13261364

13271365
acpi_gpiochip_add(chip);
13281366

1367+
machine_gpiochip_add(chip);
1368+
13291369
/*
13301370
* By first adding the chardev, and then adding the device,
13311371
* we get a device node entry in sysfs under
@@ -3462,6 +3502,33 @@ void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
34623502
}
34633503
EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
34643504

3505+
/**
3506+
* gpiod_add_hogs() - register a set of GPIO hogs from machine code
3507+
* @hogs: table of gpio hog entries with a zeroed sentinel at the end
3508+
*/
3509+
void gpiod_add_hogs(struct gpiod_hog *hogs)
3510+
{
3511+
struct gpio_chip *chip;
3512+
struct gpiod_hog *hog;
3513+
3514+
mutex_lock(&gpio_machine_hogs_mutex);
3515+
3516+
for (hog = &hogs[0]; hog->chip_label; hog++) {
3517+
list_add_tail(&hog->list, &gpio_machine_hogs);
3518+
3519+
/*
3520+
* The chip may have been registered earlier, so check if it
3521+
* exists and, if so, try to hog the line now.
3522+
*/
3523+
chip = find_chip_by_name(hog->chip_label);
3524+
if (chip)
3525+
gpiochip_machine_hog(chip, hog);
3526+
}
3527+
3528+
mutex_unlock(&gpio_machine_hogs_mutex);
3529+
}
3530+
EXPORT_SYMBOL_GPL(gpiod_add_hogs);
3531+
34653532
static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
34663533
{
34673534
const char *dev_id = dev ? dev_name(dev) : NULL;

‎include/linux/gpio/machine.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ struct gpiod_lookup_table {
3939
struct gpiod_lookup table[];
4040
};
4141

42+
/**
43+
* struct gpiod_hog - GPIO line hog table
44+
* @chip_label: name of the chip the GPIO belongs to
45+
* @chip_hwnum: hardware number (i.e. relative to the chip) of the GPIO
46+
* @line_name: consumer name for the hogged line
47+
* @lflags: mask of GPIO lookup flags
48+
* @dflags: GPIO flags used to specify the direction and value
49+
*/
50+
struct gpiod_hog {
51+
struct list_head list;
52+
const char *chip_label;
53+
u16 chip_hwnum;
54+
const char *line_name;
55+
enum gpio_lookup_flags lflags;
56+
int dflags;
57+
};
58+
4259
/*
4360
* Simple definition of a single GPIO under a con_id
4461
*/
@@ -59,17 +76,31 @@ struct gpiod_lookup_table {
5976
.flags = _flags, \
6077
}
6178

79+
/*
80+
* Simple definition of a single GPIO hog in an array.
81+
*/
82+
#define GPIO_HOG(_chip_label, _chip_hwnum, _line_name, _lflags, _dflags) \
83+
{ \
84+
.chip_label = _chip_label, \
85+
.chip_hwnum = _chip_hwnum, \
86+
.line_name = _line_name, \
87+
.lflags = _lflags, \
88+
.dflags = _dflags, \
89+
}
90+
6291
#ifdef CONFIG_GPIOLIB
6392
void gpiod_add_lookup_table(struct gpiod_lookup_table *table);
6493
void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n);
6594
void gpiod_remove_lookup_table(struct gpiod_lookup_table *table);
95+
void gpiod_add_hogs(struct gpiod_hog *hogs);
6696
#else
6797
static inline
6898
void gpiod_add_lookup_table(struct gpiod_lookup_table *table) {}
6999
static inline
70100
void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n) {}
71101
static inline
72102
void gpiod_remove_lookup_table(struct gpiod_lookup_table *table) {}
103+
static inline void gpiod_add_hogs(struct gpiod_hog *hogs) {}
73104
#endif
74105

75106
#endif /* __LINUX_GPIO_MACHINE_H */

0 commit comments

Comments
 (0)