Skip to contentSkip to conversation

gpio: set device type for GPIO chips

PATCH Superseded Bartosz Golaszewski 3 participants 5 messages Feb 1, 2024 – Feb 7, 2024 GPIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: set device type for GPIO chips

From: Bartosz Golaszewski <[email protected]>

It's useful to have the device type information for those sub-devices that are actually GPIO chips registered with GPIOLIB. While at it: use the device type struct to setup the release callback which is the preferred way to use the device API.

---
 drivers/gpio/gpiolib.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +9 -4
index d50a786f8176..6b1f16c15deb 100644
@@ -663,6 +663,11 @@ static void gpiodev_release(struct device *dev)
 	kfree(gdev);
 }
 
+static struct device_type gpio_dev_type = {
+	.name = "gpio_chip",
+	.release = gpiodev_release,
+};
+
 #ifdef CONFIG_GPIO_CDEV
 #define gcdev_register(gdev, devt)	gpiolib_cdev_register((gdev), (devt))
 #define gcdev_unregister(gdev)		gpiolib_cdev_unregister((gdev))
@@ -680,6 +685,8 @@ static int gpiochip_setup_dev(struct gpio_device *gdev)
 	struct fwnode_handle *fwnode = dev_fwnode(&gdev->dev);
 	int ret;
 
+	device_initialize(&gdev->dev);
+
 	/*
 	 * If fwnode doesn't belong to another device, it's safe to clear its
 	 * initialized flag.
@@ -691,9 +698,6 @@ static int gpiochip_setup_dev(struct gpio_device *gdev)
 	if (ret)
 		return ret;
 
-	/* From this point, the .release() function cleans up gpio_device */
-	gdev->dev.release = gpiodev_release;
-
 	ret = gpiochip_sysfs_register(gdev);
 	if (ret)
 		goto err_remove_device;
@@ -825,6 +829,8 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
 	if (!gdev)
 		return -ENOMEM;
+
+	gdev->dev.type = &gpio_dev_type;
 	gdev->dev.bus = &gpio_bus_type;
 	gdev->dev.parent = gc->parent;
 	gdev->chip = gc;
@@ -851,7 +857,6 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	if (ret)
 		goto err_free_ida;
 
-	device_initialize(&gdev->dev);
 	if (gc->parent && gc->parent->driver)
 		gdev->owner = gc->parent->driver->owner;
 	else if (gc->owner)
Signature · 1 line
2.40.1
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

It's useful to have the device type information for those sub-devices that are actually GPIO chips registered with GPIOLIB. While at it: use the device type struct to setup the release callback which is the preferred way to use the device API.

(...=

drivers/gpio/gpiolib.c static void gpiodev_release(struct device *dev)

Quoted 7 lines
drivers/gpio/gpiolib.c +5 -0
+static struct device_type gpio_dev_type = {
+       .name = "gpio_chip",
+       .release = gpiodev_release,
+};
+       gdev->dev.type = &gpio_dev_type;
        gdev->dev.bus = &gpio_bus_type;
        gdev->dev.parent = gc->parent;

Looks good to me (TM) but we should run this by Greg so he get the chance to chime in, I doubt he will see it on LKML.

FWIW:

Yours,
Linus Walleij

#2 lore raw
Quoted 4 lines · Linus Walleij
Quoted 2 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Quoted 8 lines
Quoted 6 lines

It's useful to have the device type information for those sub-devices that are actually GPIO chips registered with GPIOLIB. While at it: use the device type struct to setup the release callback which is the preferred way to use the device API.

(...=

Quoted 1 line

+static struct device_type gpio_dev_type = {

Can you make this structure const please?

Quoted 9 lines

drivers/gpio/gpiolib.c static void gpiodev_release(struct device *dev)

Quoted 6 lines
drivers/gpio/gpiolib.c +4 -0
+       .name = "gpio_chip",
+       .release = gpiodev_release,
+};
+       gdev->dev.type = &gpio_dev_type;
        gdev->dev.bus = &gpio_bus_type;
        gdev->dev.parent = gc->parent;

Looks good to me (TM) but we should run this by Greg so he get the chance to chime in, I doubt he will see it on LKML.

Yes thanks, I wouldn't have caught it...

greg k-h

#3 lore raw
Quoted 11 lines · Greg KH
Quoted 9 lines · Linus Walleij
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

It's useful to have the device type information for those sub-devices that are actually GPIO chips registered with GPIOLIB. While at it: use the device type struct to setup the release callback which is the preferred way to use the device API.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

(...=

Quoted 1 line

+static struct device_type gpio_dev_type = {

Can you make this structure const please?

I have, there's a v2 on the list already.

Thanks
Bart

Quoted 13 lines
Quoted 9 lines

drivers/gpio/gpiolib.c static void gpiodev_release(struct device *dev)

Quoted 6 lines
drivers/gpio/gpiolib.c +4 -0
+       .name = "gpio_chip",
+       .release = gpiodev_release,
+};
+       gdev->dev.type = &gpio_dev_type;
        gdev->dev.bus = &gpio_bus_type;
        gdev->dev.parent = gc->parent;

Looks good to me (TM) but we should run this by Greg so he get the chance to chime in, I doubt he will see it on LKML.

Yes thanks, I wouldn't have caught it...

greg k-h

#4 lore raw
Quoted 12 lines · Bartosz Golaszewski
Quoted 11 lines · Greg KH
Quoted 9 lines · Linus Walleij
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

It's useful to have the device type information for those sub-devices that are actually GPIO chips registered with GPIOLIB. While at it: use the device type struct to setup the release callback which is the preferred way to use the device API.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

(...=

Quoted 1 line

+static struct device_type gpio_dev_type = {

Can you make this structure const please?

I have, there's a v2 on the list already.

Great, let me go dig that up and do a review...