From patchwork Thu Jun 4 23:27:17 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergei Shtylyov X-Patchwork-Id: 480922 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id AA8421401DE for ; Fri, 5 Jun 2015 09:27:23 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753546AbbFDX1V (ORCPT ); Thu, 4 Jun 2015 19:27:21 -0400 Received: from mail-lb0-f172.google.com ([209.85.217.172]:35830 "EHLO mail-lb0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752773AbbFDX1V (ORCPT ); Thu, 4 Jun 2015 19:27:21 -0400 Received: by lbbtu8 with SMTP id tu8so20226411lbb.2 for ; Thu, 04 Jun 2015 16:27:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:organization :user-agent:in-reply-to:references:mime-version :content-transfer-encoding:content-type; bh=oi2gxBiukJAVL+hnp2EYqQ7pxVgJwEsfPG4Astz+KvQ=; b=P16on6XsedZ7QmwcQ+4gvGPx4NezuPVmMEUOQDpYes30GL18Qqs0KtGHllwu+D/Hfo aDMpmwT5/YFLEgmFhfuGkWzTxkSLtsYZJ1NMIpMvngkaobE0aDWIcIdUvwXXum+hkkXU d9eqSQ0XaT0ypXURQF16Myn1dEjSb972VQV7HqRXI49mN+Xqf+vnVVwqobzRAsfxwiU+ 5swe32d58/ShiW2x0JSGsukRRYjDmSAMPT0KRzEMDFnOPs3YQyYs0WNocaWdckkuOigv 7yWGvU/0+wxlosYfqE97xceTrf790vuxxUL89j3gnZItEkgNKMjIWT5XBQQTVuIKjhoo DmKg== X-Gm-Message-State: ALoCoQmEkHHvnXU8SspwUA3RMLq3ANm/yR/bOv8O88dZFe9nM+VjiPcscX2CVrhF7wF6d667cVvQ X-Received: by 10.112.17.68 with SMTP id m4mr508222lbd.10.1433460439416; Thu, 04 Jun 2015 16:27:19 -0700 (PDT) Received: from wasted.cogentembedded.com (ppp85-141-196-196.pppoe.mtu-net.ru. [85.141.196.196]) by mx.google.com with ESMTPSA id fb5sm1180535lbc.35.2015.06.04.16.27.18 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 Jun 2015 16:27:18 -0700 (PDT) From: Sergei Shtylyov To: linus.walleij@linaro.org, linux-sh@vger.kernel.org, laurent.pinchart@ideasonboard.com, linux-gpio@vger.kernel.org Subject: [PATCH v2 2/4] sh-pfc: handle pin array holes Date: Fri, 05 Jun 2015 02:27:17 +0300 Message-ID: <2126373.2XeHMZH1yc@wasted.cogentembedded.com> Organization: Cogent Embedded Inc. User-Agent: KMail/4.14.7 (Linux/3.19.8-100.fc20.x86_64; KDE/4.14.7; x86_64; ; ) In-Reply-To: <4326653.qPvIJDU6F2@wasted.cogentembedded.com> References: <4326653.qPvIJDU6F2@wasted.cogentembedded.com> MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org The pin array handled by sh_pfc_init_ranges() and sh_pfc_map_pins() may contain "holes" representing non-existing pins. We have to first count the pin ranges and valid pins in order to calculate the size of the memory to be allocated, then to skip over the non-existing pins when initializing the allocated arrays (we still assume that a pin at index 0 always exists). We also have to return the number of valid pins from sh_pfc_map_pins() instead of 0 on success. As we have to touch devm_kzalloc() calls in sh_pfc_map_pins() anyway, use more fitting devm_kcalloc() instead which additionally checks the array size. And since PINMUX_TYPE_NONE is #define'd as 0, stop re-initializing already zeroed out 'pmx->configs' array. While at it, add spaces around the minus signs in sh_pfc_init_ranges(). Signed-off-by: Sergei Shtylyov --- Changes in version 2: - added the handling of "holes" to sh_pfc_init_ranges(); - renamed the 'n' local variable in sh_pfc_map_pins() to 'num_pins' and declared it on a separate line; - moved the 'info' local variable inside 2nd loop in sh_pfc_map_pins(); - added comment before the 2nd devm_kcalloc() call in sh_pfc_map_pins(); - added comment to the 2nd loop in sh_pfc_map_pins(); - renamed and refreshed the patch. drivers/pinctrl/sh-pfc/core.c | 29 +++++++++++++++++++---------- drivers/pinctrl/sh-pfc/pinctrl.c | 29 +++++++++++++++++++---------- 2 files changed, 38 insertions(+), 20 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-gpio" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Index: linux-pinctrl/drivers/pinctrl/sh-pfc/core.c =================================================================== --- linux-pinctrl.orig/drivers/pinctrl/sh-pfc/core.c +++ linux-pinctrl/drivers/pinctrl/sh-pfc/core.c @@ -405,7 +405,10 @@ static int sh_pfc_init_ranges(struct sh_ * last. */ for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) { - if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1) + /* Check for the "holes" first */ + if (!pfc->info->pins[i].enum_id && !pfc->info->pins[i].configs) + continue; + if (pfc->info->pins[i - 1].pin != pfc->info->pins[i].pin - 1) nr_ranges++; } @@ -419,19 +422,25 @@ static int sh_pfc_init_ranges(struct sh_ range->start = pfc->info->pins[0].pin; for (i = 1; i < pfc->info->nr_pins; ++i) { - if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1) + if (pfc->info->pins[i - 1].pin == pfc->info->pins[i].pin - 1) continue; - range->end = pfc->info->pins[i-1].pin; - if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO)) - pfc->nr_gpio_pins = range->end + 1; - - range++; - range->start = pfc->info->pins[i].pin; + if (pfc->info->pins[i - 1].enum_id || + pfc->info->pins[i - 1].configs) { + range->end = pfc->info->pins[i - 1].pin; + if (!(pfc->info->pins[i - 1].configs & + SH_PFC_PIN_CFG_NO_GPIO)) + pfc->nr_gpio_pins = range->end + 1; + } + + if (pfc->info->pins[i].enum_id || pfc->info->pins[i].configs) { + range++; + range->start = pfc->info->pins[i].pin; + } } - range->end = pfc->info->pins[i-1].pin; - if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO)) + range->end = pfc->info->pins[i - 1].pin; + if (!(pfc->info->pins[i - 1].configs & SH_PFC_PIN_CFG_NO_GPIO)) pfc->nr_gpio_pins = range->end + 1; return 0; Index: linux-pinctrl/drivers/pinctrl/sh-pfc/pinctrl.c =================================================================== --- linux-pinctrl.orig/drivers/pinctrl/sh-pfc/pinctrl.c +++ linux-pinctrl/drivers/pinctrl/sh-pfc/pinctrl.c @@ -570,33 +570,42 @@ static const struct pinconf_ops sh_pfc_p /* PFC ranges -> pinctrl pin descs */ static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx) { + struct pinctrl_pin_desc *pin; + unsigned int num_pins; unsigned int i; + /* Count the valid pins. */ + for (i = 0, num_pins = 0; i < pfc->info->nr_pins; i++) { + if (pfc->info->pins[i].enum_id || pfc->info->pins[i].configs) + num_pins++; + } + /* Allocate and initialize the pins and configs arrays. */ - pmx->pins = devm_kzalloc(pfc->dev, - sizeof(*pmx->pins) * pfc->info->nr_pins, + pmx->pins = devm_kcalloc(pfc->dev, num_pins, sizeof(*pmx->pins), GFP_KERNEL); if (unlikely(!pmx->pins)) return -ENOMEM; - pmx->configs = devm_kzalloc(pfc->dev, - sizeof(*pmx->configs) * pfc->info->nr_pins, + /* Equivalent to setting the type field to PINMUX_TYPE_NONE */ + pmx->configs = devm_kcalloc(pfc->dev, num_pins, sizeof(*pmx->configs), GFP_KERNEL); if (unlikely(!pmx->configs)) return -ENOMEM; - for (i = 0; i < pfc->info->nr_pins; ++i) { + for (i = 0, pin = pmx->pins; i < pfc->info->nr_pins; i++) { const struct sh_pfc_pin *info = &pfc->info->pins[i]; - struct sh_pfc_pin_config *cfg = &pmx->configs[i]; - struct pinctrl_pin_desc *pin = &pmx->pins[i]; + + /* Skip the "hole" */ + if (!info->enum_id && !info->configs) + continue; /* If the pin number is equal to -1 all pins are considered */ pin->number = info->pin != (u16)-1 ? info->pin : i; pin->name = info->name; - cfg->type = PINMUX_TYPE_NONE; + pin++; } - return 0; + return num_pins; } int sh_pfc_register_pinctrl(struct sh_pfc *pfc) @@ -621,7 +630,7 @@ int sh_pfc_register_pinctrl(struct sh_pf pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops; pmx->pctl_desc.confops = &sh_pfc_pinconf_ops; pmx->pctl_desc.pins = pmx->pins; - pmx->pctl_desc.npins = pfc->info->nr_pins; + pmx->pctl_desc.npins = ret; pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx); if (pmx->pctl == NULL)