#include <linux/gpio/driver.h>
 #include <linux/gpio/regmap.h>
 
+#include "gpiolib.h"
+
 struct gpio_regmap {
        struct device *parent;
        struct regmap *regmap;
        if (!config->parent)
                return ERR_PTR(-EINVAL);
 
-       if (!config->ngpio)
-               return ERR_PTR(-EINVAL);
-
        /* we need at least one */
        if (!config->reg_dat_base && !config->reg_set_base)
                return ERR_PTR(-EINVAL);
        chip->parent = config->parent;
        chip->fwnode = config->fwnode;
        chip->base = -1;
-       chip->ngpio = config->ngpio;
        chip->names = config->names;
        chip->label = config->label ?: dev_name(config->parent);
        chip->can_sleep = regmap_might_sleep(config->regmap);
                chip->direction_output = gpio_regmap_direction_output;
        }
 
+       chip->ngpio = config->ngpio;
+       if (!chip->ngpio) {
+               ret = gpiochip_get_ngpios(chip, chip->parent);
+               if (ret)
+                       return ERR_PTR(ret);
+       }
+
        /* if not set, assume there is only one register */
        gpio->ngpio_per_reg = config->ngpio_per_reg;
        if (!gpio->ngpio_per_reg)
 
  *                     If not given, the fwnode of the parent is used.
  * @label:             (Optional) Descriptive name for GPIO controller.
  *                     If not given, the name of the device is used.
- * @ngpio:             Number of GPIOs
+ * @ngpio:             (Optional) Number of GPIOs
  * @names:             (Optional) Array of names for gpios
  * @reg_dat_base:      (Optional) (in) register base address
  * @reg_set_base:      (Optional) set register base address