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: 6550541 X-Patchwork-Delegate: geert@linux-m68k.org Return-Path: X-Original-To: patchwork-linux-sh@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id E2785C0020 for ; Thu, 4 Jun 2015 23:27:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D237120702 for ; Thu, 4 Jun 2015 23:27:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BE328206F8 for ; Thu, 4 Jun 2015 23:27:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753750AbbFDX1W (ORCPT ); Thu, 4 Jun 2015 19:27:22 -0400 Received: from mail-lb0-f180.google.com ([209.85.217.180]:33545 "EHLO mail-lb0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753019AbbFDX1V (ORCPT ); Thu, 4 Jun 2015 19:27:21 -0400 Received: by lbcue7 with SMTP id ue7so36284338lbc.0 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=knu1NlX4qFgTLTXAUe0BeOfxA/3vjyKTR+04IlfInMQjg6IkJsdjjleIOBhvkyZ7oo E2PKfDKECZyURA7y6VCaI2iV12TPw7A5srTlIPEJKlazeKPqhg86W3n0+XAKWvVZkq6W Bj0u2AZMVk4qlx0oI6izjwXHpGOOG3Xg7zWbhbt7DXJgK4Nitc5z/bMiywcOAeKvoUXf ojOroO66FgATXAXON22icfMA4u3Kbq6guC6NdoSiOtNZ7cQ07ghgd/7UZ37EWaNbOH69 2vRMJUolTUkGVMD0cfxK5O7CjC7gL8CMe9z78fttydj3rl2Z7lnCmhU3s0lYBOANxGJU j1Ww== X-Gm-Message-State: ALoCoQmfYyMOr6q4vFzBUTjvGjUFeswLg6WLz7DQMf3iLZc8hucXU5DVfckIXmbASesE7g1J4Tth 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-sh-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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-sh" 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)