Skip to content

Commit 370232d

Browse files
author
Bartosz Golaszewski
committed
gpiolib: provide gpiod_to_gpio_device()
Accessing struct gpio_chip backing a GPIO device is only allowed for the actual providers of that chip. Similarly to how we introduced gpio_device_find() in order to replace the abused gpiochip_find(), let's introduce a counterpart to gpiod_to_chip() that returns a reference to the GPIO device owning the descriptor. This is done in order to later remove gpiod_to_chip() entirely. Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org> Reviewed-by: Peter Rosin <peda@axentia.se> Acked-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 1559d14 commit 370232d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

drivers/gpio/gpiolib.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,27 @@ struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
220220
}
221221
EXPORT_SYMBOL_GPL(gpiod_to_chip);
222222

223+
/**
224+
* gpiod_to_gpio_device() - Return the GPIO device to which this descriptor
225+
* belongs.
226+
* @desc: Descriptor for which to return the GPIO device.
227+
*
228+
* This *DOES NOT* increase the reference count of the GPIO device as it's
229+
* expected that the descriptor is requested and the users already holds a
230+
* reference to the device.
231+
*
232+
* Returns:
233+
* Address of the GPIO device owning this descriptor.
234+
*/
235+
struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc)
236+
{
237+
if (!desc)
238+
return NULL;
239+
240+
return desc->gdev;
241+
}
242+
EXPORT_SYMBOL_GPL(gpiod_to_gpio_device);
243+
223244
/**
224245
* gpio_device_get_chip() - Get the gpio_chip implementation of this GPIO device
225246
* @gdev: GPIO device

include/linux/gpio/driver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset);
785785
void gpiochip_unlock_as_irq(struct gpio_chip *gc, unsigned int offset);
786786

787787
struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc);
788+
struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc);
788789

789790
#else /* CONFIG_GPIOLIB */
790791

0 commit comments

Comments
 (0)