mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	gpio: aggregator: refactor the code to add GPIO desc in the forwarder
Create a dedicated function to add a GPIO desc in the forwarder. Instead of saving a GPIO descs array pointer, now the GPIO descs are passed one by one to the forwarder which registers them in its own array. So after the call of gpiochip_fwd_create(), the passed array can be free. Reviewed-by: Andy Shevchenko <andy@kernel.org> Acked-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Thomas Richard <thomas.richard@bootlin.com> Link: https://lore.kernel.org/r/20250811-aaeon-up-board-pinctrl-support-v9-3-29f0cbbdfb30@bootlin.com Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
This commit is contained in:
		
							parent
							
								
									871c7cd548
								
							
						
					
					
						commit
						c44ce91b8a
					
				
					 1 changed files with 40 additions and 19 deletions
				
			
		| 
						 | 
					@ -485,6 +485,10 @@ devm_gpiochip_fwd_alloc(struct device *dev, unsigned int ngpios)
 | 
				
			||||||
	if (!fwd)
 | 
						if (!fwd)
 | 
				
			||||||
		return ERR_PTR(-ENOMEM);
 | 
							return ERR_PTR(-ENOMEM);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fwd->descs = devm_kcalloc(dev, ngpios, sizeof(*fwd->descs), GFP_KERNEL);
 | 
				
			||||||
 | 
						if (!fwd->descs)
 | 
				
			||||||
 | 
							return ERR_PTR(-ENOMEM);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	chip = &fwd->chip;
 | 
						chip = &fwd->chip;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	chip->label = dev_name(dev);
 | 
						chip->label = dev_name(dev);
 | 
				
			||||||
| 
						 | 
					@ -504,13 +508,43 @@ devm_gpiochip_fwd_alloc(struct device *dev, unsigned int ngpios)
 | 
				
			||||||
	return fwd;
 | 
						return fwd;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int gpiochip_fwd_desc_add(struct gpiochip_fwd *fwd,
 | 
				
			||||||
 | 
									 struct gpio_desc *desc,
 | 
				
			||||||
 | 
									 unsigned int offset)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct gpio_chip *parent = gpiod_to_chip(desc);
 | 
				
			||||||
 | 
						struct gpio_chip *chip = &fwd->chip;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (offset > chip->ngpio)
 | 
				
			||||||
 | 
							return -EINVAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * If any of the GPIO lines are sleeping, then the entire forwarder
 | 
				
			||||||
 | 
						 * will be sleeping.
 | 
				
			||||||
 | 
						 * If any of the chips support .set_config(), then the forwarder will
 | 
				
			||||||
 | 
						 * support setting configs.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						if (gpiod_cansleep(desc))
 | 
				
			||||||
 | 
							chip->can_sleep = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (parent && parent->set_config)
 | 
				
			||||||
 | 
							chip->set_config = gpio_fwd_set_config;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fwd->descs[offset] = desc;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dev_dbg(chip->parent, "%u => gpio %d irq %d\n", offset,
 | 
				
			||||||
 | 
							desc_to_gpio(desc), gpiod_to_irq(desc));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * gpiochip_fwd_create() - Create a new GPIO forwarder
 | 
					 * gpiochip_fwd_create() - Create a new GPIO forwarder
 | 
				
			||||||
 * @dev: Parent device pointer
 | 
					 * @dev: Parent device pointer
 | 
				
			||||||
 * @ngpios: Number of GPIOs in the forwarder.
 | 
					 * @ngpios: Number of GPIOs in the forwarder.
 | 
				
			||||||
 * @descs: Array containing the GPIO descriptors to forward to.
 | 
					 * @descs: Array containing the GPIO descriptors to forward to.
 | 
				
			||||||
 *         This array must contain @ngpios entries, and must not be deallocated
 | 
					 *         This array must contain @ngpios entries, and can be deallocated
 | 
				
			||||||
 *         before the forwarder has been destroyed again.
 | 
					 *         as the forwarder has its own array.
 | 
				
			||||||
 * @features: Bitwise ORed features as defined with FWD_FEATURE_*.
 | 
					 * @features: Bitwise ORed features as defined with FWD_FEATURE_*.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * This function creates a new gpiochip, which forwards all GPIO operations to
 | 
					 * This function creates a new gpiochip, which forwards all GPIO operations to
 | 
				
			||||||
| 
						 | 
					@ -535,26 +569,12 @@ static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	chip = &fwd->chip;
 | 
						chip = &fwd->chip;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/*
 | 
					 | 
				
			||||||
	 * If any of the GPIO lines are sleeping, then the entire forwarder
 | 
					 | 
				
			||||||
	 * will be sleeping.
 | 
					 | 
				
			||||||
	 * If any of the chips support .set_config(), then the forwarder will
 | 
					 | 
				
			||||||
	 * support setting configs.
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	for (i = 0; i < ngpios; i++) {
 | 
						for (i = 0; i < ngpios; i++) {
 | 
				
			||||||
		struct gpio_chip *parent = gpiod_to_chip(descs[i]);
 | 
							error = gpiochip_fwd_desc_add(fwd, descs[i], i);
 | 
				
			||||||
 | 
							if (error)
 | 
				
			||||||
		dev_dbg(dev, "%u => gpio %d irq %d\n", i,
 | 
								return ERR_PTR(error);
 | 
				
			||||||
			desc_to_gpio(descs[i]), gpiod_to_irq(descs[i]));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (gpiod_cansleep(descs[i]))
 | 
					 | 
				
			||||||
			chip->can_sleep = true;
 | 
					 | 
				
			||||||
		if (parent && parent->set_config)
 | 
					 | 
				
			||||||
			chip->set_config = gpio_fwd_set_config;
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fwd->descs = descs;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if (chip->can_sleep)
 | 
						if (chip->can_sleep)
 | 
				
			||||||
		mutex_init(&fwd->mlock);
 | 
							mutex_init(&fwd->mlock);
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
| 
						 | 
					@ -1348,6 +1368,7 @@ static int gpio_aggregator_probe(struct platform_device *pdev)
 | 
				
			||||||
		return PTR_ERR(fwd);
 | 
							return PTR_ERR(fwd);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	platform_set_drvdata(pdev, fwd);
 | 
						platform_set_drvdata(pdev, fwd);
 | 
				
			||||||
 | 
						devm_kfree(dev, descs);
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue