pinctrl: intel: Split intel_pinctrl_add_padgroups() for better maintenance
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Thu, 7 Jan 2021 19:01:57 +0000 (21:01 +0200)
committerAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Fri, 8 Jan 2021 12:57:16 +0000 (14:57 +0200)
Currently the intel_pinctrl_add_padgroups() is twisted a bit due to
a different nature of the pin control hardware implementations. Thus,
its maintenance is a bit hard. Besides that some pieces of code
are run on all hardware and make this code slightly inefficient,
and moreover, validation for one case is done in a wrong time in a flow
which makes it even slower.

Split intel_pinctrl_add_padgroups() to two functions, one per hardware
implementation, for better maintenance and readability.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Acked-by: Mika Westerberg <mika.westerberg@linux.intel.com>
drivers/pinctrl/intel/pinctrl-intel.c

index b6ef1911c1dd16a3060a1e95d08e21f41d3fe8d3..ae13e43909358fae317464dacc07b64f977f46e9 100644 (file)
@@ -1321,34 +1321,19 @@ static int intel_gpio_probe(struct intel_pinctrl *pctrl, int irq)
        return 0;
 }
 
-static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
-                                      struct intel_community *community)
+static int intel_pinctrl_add_padgroups_by_gpps(struct intel_pinctrl *pctrl,
+                                              struct intel_community *community)
 {
        struct intel_padgroup *gpps;
-       unsigned int npins = community->npins;
        unsigned int padown_num = 0;
-       size_t ngpps, i;
-
-       if (community->gpps)
-               ngpps = community->ngpps;
-       else
-               ngpps = DIV_ROUND_UP(community->npins, community->gpp_size);
+       size_t i, ngpps = community->ngpps;
 
        gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
        if (!gpps)
                return -ENOMEM;
 
        for (i = 0; i < ngpps; i++) {
-               if (community->gpps) {
-                       gpps[i] = community->gpps[i];
-               } else {
-                       unsigned int gpp_size = community->gpp_size;
-
-                       gpps[i].reg_num = i;
-                       gpps[i].base = community->pin_base + i * gpp_size;
-                       gpps[i].size = min(gpp_size, npins);
-                       npins -= gpps[i].size;
-               }
+               gpps[i] = community->gpps[i];
 
                if (gpps[i].size > 32)
                        return -EINVAL;
@@ -1366,6 +1351,38 @@ static int intel_pinctrl_add_padgroups(struct intel_pinctrl *pctrl,
                                break;
                }
 
+               gpps[i].padown_num = padown_num;
+               padown_num += DIV_ROUND_UP(gpps[i].size * 4, 32);
+       }
+
+       community->gpps = gpps;
+
+       return 0;
+}
+
+static int intel_pinctrl_add_padgroups_by_size(struct intel_pinctrl *pctrl,
+                                              struct intel_community *community)
+{
+       struct intel_padgroup *gpps;
+       unsigned int npins = community->npins;
+       unsigned int padown_num = 0;
+       size_t i, ngpps = DIV_ROUND_UP(npins, community->gpp_size);
+
+       if (community->gpp_size > 32)
+               return -EINVAL;
+
+       gpps = devm_kcalloc(pctrl->dev, ngpps, sizeof(*gpps), GFP_KERNEL);
+       if (!gpps)
+               return -ENOMEM;
+
+       for (i = 0; i < ngpps; i++) {
+               unsigned int gpp_size = community->gpp_size;
+
+               gpps[i].reg_num = i;
+               gpps[i].base = community->pin_base + i * gpp_size;
+               gpps[i].size = min(gpp_size, npins);
+               npins -= gpps[i].size;
+
                gpps[i].padown_num = padown_num;
 
                /*
@@ -1483,7 +1500,10 @@ static int intel_pinctrl_probe(struct platform_device *pdev,
                community->regs = regs;
                community->pad_regs = regs + padbar;
 
-               ret = intel_pinctrl_add_padgroups(pctrl, community);
+               if (community->gpps)
+                       ret = intel_pinctrl_add_padgroups_by_gpps(pctrl, community);
+               else
+                       ret = intel_pinctrl_add_padgroups_by_size(pctrl, community);
                if (ret)
                        return ret;
        }