From patchwork Tue Feb 25 19:40:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 868274 Received: from mail-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAE911EBFE6 for ; Tue, 25 Feb 2025 19:40:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740512440; cv=none; b=J3OZlH5l2cvaMwlHi9m4TnQNAw8HB2AelvkjPIcdz+FnMsHnx31tJJ/oUHgwyWUb5AQpcYxY4iCs5KNMqEVDfxUFtp4bSseFVdwERTl8sOx4ktcaGkQwxcudmjGFJA6GJ7UB2hnUKLS+8jyuIxeenCQ4bOwyrMIwIs2fvM0jHG4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740512440; c=relaxed/simple; bh=JnkVoJVEvt9wSbfZJwKuTmFUsqCBtk0hMWljeyVPBxA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=E5VfCwjC1GM2rfBW2LwRIJqRBFXlN8+Z5ul73OW2k3P+wFA9FzJPrh3dOkskr7CDX0enPbeb+pRZ2LNPgqNdPdeSp/da/1ow7r8Kic5gT0ZA/woxMeMuFYHCu+SL4FPkjCxm3bIV4gYbYFIgjcqttF0DgysWHo/Bbf6Fic8ifz4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=mbGE39Dr; arc=none smtp.client-ip=209.85.208.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="mbGE39Dr" Received: by mail-lj1-f178.google.com with SMTP id 38308e7fff4ca-30795988ebeso62708921fa.3 for ; Tue, 25 Feb 2025 11:40:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1740512437; x=1741117237; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=MeJ7a/9Q7Y4DFP2keHrgY23a3sacV+V2x1QKfVZGDtA=; b=mbGE39Dr1tWrw4kNUYKlPfFbtUf1X2f+mtR9hZh7h2TsTbtSqmsyaCDoPCE8eMfb/4 +02jEnPs6u5EvBXpaqPDQsDx/EV+sXA8kGZyh3mP59W0767HBIZEAEoiH6frOteKvyJR PrlM5Ld6/Sp885+VArpRCnS7Xg2wIKeCVWVnClB743b3nG75yHAgQF6Jpq8He8d68Ew9 GkpRjR6xATOeR+rZBTH3cTIDol6xfb5rsRA08m5ZwLYuJ+CBDe+KnyEL/F4cu7L2nt/H xil9fU7yqVU/hfWI6VtPp23Jw4vGr9HWmq6Mx+jyNFq+XusLIfyET1ohywYDRUG7Re7W g5Aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740512437; x=1741117237; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MeJ7a/9Q7Y4DFP2keHrgY23a3sacV+V2x1QKfVZGDtA=; b=wdBACoznn2KpdOK2Gd134zDebafZKUdGaXwMiDZ6fUgpRQVRImxrbxgSjjjCH9wdbG NxkMnjtF5sNlxSIncO1Bzam9VgYTUbPYlUPrDZR+BjwTdvUFL9VpIVXJqvDyzVYc8j6i tCU82Iotx3Yb1j2r/7oryMLB4kgqG0ZkW11qiVbj92fIHISZoYfKQ7TD5xI8DFW8LnHF 610oRWZ5QuflKuQjgQ3UTrYtDCMVFgq8IL2G1/Mqimc3FeuNb+uhmtXiOiGqhDSeOxS6 yN0z3+Wdf5Qe+bKkGrtz/slFq2rr4iVyxOBsF347WwvIEW8TUDN4CnHaDYFengXDDjK8 gfIA== X-Forwarded-Encrypted: i=1; AJvYcCUqV1iZf9BhdX3v/uws4lCpsc4sNVkrBIy/6YcA7p6Kos2CNtdg5y0MkM5TFFYHaLtFbHw8Y9LwxMho@vger.kernel.org X-Gm-Message-State: AOJu0YxB27g1dlnhYL9mMOvc9DZxhpUmPKfatzMgmgR4c9NsHK/YvEFJ Zzw8P60Rx2CbpqFYLoZDH4Oc65mBEgE82jRnKyxa0yrgIX94ZSnWxtF4j26XpD0= X-Gm-Gg: ASbGnctp6zHwDi86oeUY9r6ZsWLit/05987FKaDz7PzNo0EvLGtRI27GRwJFEkxX3B2 oJeDoGTJdDex1Ckq25K9ykK9WRpD0xyWwIUeZv9vAbbSP1p5xOrGIVGVQRrsjW3EtyiAumydMwZ tNpSeyP0KhyHIsWEMHbizHdyvURk6paDgeMHfza6LOIY8Tkyh4xFQ4Xyu5HjrQp6D0pu/kKHO32 2V4BLZPWQABpOuCLI2fVgEioi5QaQ/sIuObpMjk8MVWTvXyYOijMmr3vMRuHmMhcwaYS1GFPGxr axMHWSRze/vzuHJ4hee/xGYCLNS0NyLKQI4n X-Google-Smtp-Source: AGHT+IFTqWSZomOSuAP/ATSSMRp65Ny/RXKj63O0ukDsYSnOg9Uk8nYn334WNgPzDBBDjL7898oKJw== X-Received: by 2002:a2e:321a:0:b0:309:bc3:36b4 with SMTP id 38308e7fff4ca-30a80c0f0c9mr22439111fa.6.1740512436920; Tue, 25 Feb 2025 11:40:36 -0800 (PST) Received: from [192.168.1.140] ([85.235.12.238]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-30a818164e2sm3089721fa.0.2025.02.25.11.40.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2025 11:40:35 -0800 (PST) From: Linus Walleij Date: Tue, 25 Feb 2025 20:40:33 +0100 Subject: [PATCH v3 1/2] gpiolib: of: Use local variables Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250225-gpio-ranges-fourcell-v3-1-860382ba4713@linaro.org> References: <20250225-gpio-ranges-fourcell-v3-0-860382ba4713@linaro.org> In-Reply-To: <20250225-gpio-ranges-fourcell-v3-0-860382ba4713@linaro.org> To: Yixun Lan , Bartosz Golaszewski , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: devicetree@vger.kernel.org, linux-gpio@vger.kernel.org, Linus Walleij , Alex Elder X-Mailer: b4 0.14.2 Instead of modifying the contents of the array of values read in from a phandle, use local variables to store the values. This makes the code easier to read and the array immutable. Reviewed-by: Alex Elder Tested-by: Yixun Lan Signed-off-by: Linus Walleij --- drivers/gpio/gpiolib-of.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c index 2e537ee979f3e2b6e8d5f86f3e121a66f2a8e083..86405218f4e2ddc951a1a9d168e886400652bf60 100644 --- a/drivers/gpio/gpiolib-of.c +++ b/drivers/gpio/gpiolib-of.c @@ -1057,6 +1057,9 @@ static int of_gpiochip_add_pin_range(struct gpio_chip *chip) const char *name; static const char group_names_propname[] = "gpio-ranges-group-names"; bool has_group_names; + int offset; /* Offset of the first GPIO line on the chip */ + int pin; /* Pin base number in the range */ + int count; /* Number of pins/GPIO lines to map */ np = dev_of_node(&chip->gpiodev->dev); if (!np) @@ -1075,13 +1078,17 @@ static int of_gpiochip_add_pin_range(struct gpio_chip *chip) if (!pctldev) return -EPROBE_DEFER; + offset = pinspec.args[0]; + pin = pinspec.args[1]; + count = pinspec.args[2]; + /* Ignore ranges outside of this GPIO chip */ - if (pinspec.args[0] >= (chip->offset + chip->ngpio)) + if (offset >= (chip->offset + chip->ngpio)) continue; - if (pinspec.args[0] + pinspec.args[2] <= chip->offset) + if (offset + count <= chip->offset) continue; - if (pinspec.args[2]) { + if (count) { /* npins != 0: linear range */ if (has_group_names) { of_property_read_string_index(np, @@ -1095,27 +1102,27 @@ static int of_gpiochip_add_pin_range(struct gpio_chip *chip) } /* Trim the range to fit this GPIO chip */ - if (chip->offset > pinspec.args[0]) { - trim = chip->offset - pinspec.args[0]; - pinspec.args[2] -= trim; - pinspec.args[1] += trim; - pinspec.args[0] = 0; + if (chip->offset > offset) { + trim = chip->offset - offset; + count -= trim; + pin += trim; + offset = 0; } else { - pinspec.args[0] -= chip->offset; + offset -= chip->offset; } - if ((pinspec.args[0] + pinspec.args[2]) > chip->ngpio) - pinspec.args[2] = chip->ngpio - pinspec.args[0]; + if ((offset + count) > chip->ngpio) + count = chip->ngpio - offset; ret = gpiochip_add_pin_range(chip, pinctrl_dev_get_devname(pctldev), - pinspec.args[0], - pinspec.args[1], - pinspec.args[2]); + offset, + pin, + count); if (ret) return ret; } else { /* npins == 0: special range */ - if (pinspec.args[1]) { + if (pin) { pr_err("%pOF: Illegal gpio-range format.\n", np); break; @@ -1140,7 +1147,7 @@ static int of_gpiochip_add_pin_range(struct gpio_chip *chip) } ret = gpiochip_add_pingroup_range(chip, pctldev, - pinspec.args[0], name); + offset, name); if (ret) return ret; } From patchwork Tue Feb 25 19:40:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 868638 Received: from mail-lj1-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC55A207DFF for ; Tue, 25 Feb 2025 19:40:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740512442; cv=none; b=uoYcCdF/0YL4t4R6JoT2HD7I6v/VLhbKRcfukK9z1bLTBdKF9eCvx6E6qnk9UV/cLD9Fqn6e8Ic3eoN8hTYNhVWxy+euKJP6Gz24pqW8JsQ5SSpoKY5fdHBYNaGVJjK5wJzet3ljdZbfe7Tcc9CsB+L6JFNltI7V6tJgBdHIYW0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740512442; c=relaxed/simple; bh=yAlbUHu9L3oaIcbOjw3lFRnS49fEs9Yeu+kdf4WIgGY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KQkFe8qm5wujJ0gX9MFNryytEip0HZqYjgNn3Hv5mRgpZOhqRRduTNokTfeiMOGKz1lDUHgf/O04BHtWMXKq80kZLZymKeZpyvTKpgV3Rp9Gk5rdX6JlqXEBDFbzuHnQNuZfQo1q2M83spSIbnhFoghl+oah2bZ6BvfcZpOIPc0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=oNOmL0Us; arc=none smtp.client-ip=209.85.208.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="oNOmL0Us" Received: by mail-lj1-f179.google.com with SMTP id 38308e7fff4ca-30761be8fa7so56057491fa.2 for ; Tue, 25 Feb 2025 11:40:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1740512439; x=1741117239; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=5xiO6N8n+C89NvfyEvDJqqu2/WiWa8biLzHBP2fNTm4=; b=oNOmL0UsrswIzyhql1BbOWsUuOZGOCaHVFeoWtHthrJ6f1WjyhB5EtALK0o82Ri8Kw XaMidaKE+IeDsaOcCmHNUHRK4seMgVoDTIxG6lrJ9/g2D/nCaiA4KOHbrLfCLKcZ+Mnp zH0pQqWssoImDFtRnNFd/PHjbQSCd3zD6q6RUmLoBeTPr3+X7SF2u3kMp4qzwQ23hQGU mtXEM+QGIzfTw0qZ1K3SX2u4zmJI0zx0KM37nASI2+7Bl6AXPLITdbIvaFP+G6Tvfs4h ktNBltOwXHzbRyf6kHnRf/hji+NhAzSVKnL47j9yi/RQOFLe8Q46YA4bQMat5jvn5YwT kxdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740512439; x=1741117239; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5xiO6N8n+C89NvfyEvDJqqu2/WiWa8biLzHBP2fNTm4=; b=merDpRhALr/JnVWg22R8KGBc2Sq1Za5jmGEcXAQ2V7dW7JPjxS4hNVmarat28t6GXa sVeFxjBFRmY6MhtQI0CT+sauSiyfSdGYatb0x5WYLMioxGsNvMA3h5hy+IpZ0XrkXZYQ JtUyRVYeQ8GENMtLiaz9NybkkTdOLYKx/dGVSmymeKfeJbt8/R1GN7RErvpuVtbL0YPd Kkww5CUzepBgIBoCSDnqu/dmfnfV0ehrBR+1S0GRqkLDu0H1sS54mVnkjLTxL5SQlYlv TlRvtS2Le1aqVRgTX64Fcq+dsKW7OOTjqJWGwJV7zrRw7LbkUH7JF0PiPkr7DBnZ3WgV FlnQ== X-Forwarded-Encrypted: i=1; AJvYcCUVsbtKQ3iV3G03zMKs1tp5Q9B8bRvPTN0GhMtMFn5cjSljRlqK7wXJVZdQ3L3fLVCTvSvmvNlrAf7e@vger.kernel.org X-Gm-Message-State: AOJu0YxZK5ddJDjdEysNvgVfpsUBWilrJmo71Zfyo+UVVTcUoQ0RExlu /tZZoMhY2KvOx5pv4AkJUxZ/HoE3Pf8GG9BdzCHMUVmGWk7JghJN6olq3qmHn90= X-Gm-Gg: ASbGncvlcfjj+iGyKnwklumlnQAv3mWtheOVrKadvgkXvxu3jBNkk0o10bvDXMPKiz5 F3OapoY2KyoOFI9l7H55v/LmcV2Rb2k9Bf6XawAVbSWeDTY5+0yLHNKMeiujESuwlcvLYbktIuF wx16aYmiDnpfKmCnq+73M83bh6xadfttw8xLZnpAxsgLrkV/UfxipUlv2A1NS6vKFyEeA/ga+qU JnepYCotbqx0XKPRIG7oLC0xEMnw0lhFR/MqDcjpe7pjPJu+ZiYWPmz6wgp3ICG3N5CfSLUYrs8 qgCbzV+1ZUwiaYI7z47o0gvPdG+qwzdLd2Vp X-Google-Smtp-Source: AGHT+IFiHhNPLht6EoyGBj0FIiFN7PLl2rLeoGOMMONf0/5dMXM05I/rAF+ggyngtRFbpAn4hOVwuw== X-Received: by 2002:a2e:9909:0:b0:309:bc3:3a71 with SMTP id 38308e7fff4ca-30b792e81edmr7540611fa.31.1740512438637; Tue, 25 Feb 2025 11:40:38 -0800 (PST) Received: from [192.168.1.140] ([85.235.12.238]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-30a818164e2sm3089721fa.0.2025.02.25.11.40.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2025 11:40:37 -0800 (PST) From: Linus Walleij Date: Tue, 25 Feb 2025 20:40:34 +0100 Subject: [PATCH v3 2/2] gpiolib: of: Handle threecell GPIO chips Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250225-gpio-ranges-fourcell-v3-2-860382ba4713@linaro.org> References: <20250225-gpio-ranges-fourcell-v3-0-860382ba4713@linaro.org> In-Reply-To: <20250225-gpio-ranges-fourcell-v3-0-860382ba4713@linaro.org> To: Yixun Lan , Bartosz Golaszewski , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: devicetree@vger.kernel.org, linux-gpio@vger.kernel.org, Linus Walleij , Alex Elder X-Mailer: b4 0.14.2 When describing GPIO controllers in the device tree, the ambition of device tree to describe the hardware may require a three-cell scheme: gpios = <&gpio instance offset flags>; This implements support for this scheme in the gpiolib OF core. Drivers that want to handle multiple gpiochip instances from one OF node need to implement a callback similar to this to determine if a certain gpio chip is a pointer to the right instance (pseudo-code): struct my_gpio { struct gpio_chip gcs[MAX_CHIPS]; }; static bool my_of_node_instance_match(struct gpio_chip *gc unsigned int instance) { struct my_gpio *mg = gpiochip_get_data(gc); if (instance >= MAX_CHIPS) return false; return (gc == &mg->gcs[instance]); } probe() { struct my_gpio *mg; struct gpio_chip *gc; int i, ret; for (i = 0; i++; i < MAX_CHIPS) { gc = &mg->gcs[i]; /* This tells gpiolib we have several instances per node */ gc->of_gpio_n_cells = 3; gc->of_node_instance_match = my_of_node_instance_match; gc->base = -1; ... ret = devm_gpiochip_add_data(dev, gc, mg); if (ret) return ret; } } Rename the "simple" of_xlate function to "twocell" which is closer to what it actually does. In the device tree bindings, the provide node needs to specify #gpio-cells = <3>; where the first cell is the instance number: gpios = <&gpio instance offset flags>; Conversely ranges need to have four cells: gpio-ranges = <&pinctrl instance gpio_offset pin_offset count>; Reviewed-by: Alex Elder Tested-by: Yixun Lan Signed-off-by: Linus Walleij --- drivers/gpio/gpiolib-of.c | 95 +++++++++++++++++++++++++++++++++++++++------ include/linux/gpio/driver.h | 24 +++++++++++- 2 files changed, 107 insertions(+), 12 deletions(-) diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c index 86405218f4e2ddc951a1a9d168e886400652bf60..6e0eb67dcbf05e653f329cd97d09bc3b773e2c56 100644 --- a/drivers/gpio/gpiolib-of.c +++ b/drivers/gpio/gpiolib-of.c @@ -929,7 +929,7 @@ struct notifier_block gpio_of_notifier = { #endif /* CONFIG_OF_DYNAMIC */ /** - * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags + * of_gpio_twocell_xlate - translate twocell gpiospec to the GPIO number and flags * @gc: pointer to the gpio_chip structure * @gpiospec: GPIO specifier as found in the device tree * @flags: a flags pointer to fill in @@ -941,9 +941,9 @@ struct notifier_block gpio_of_notifier = { * Returns: * GPIO number (>= 0) on success, negative errno on failure. */ -static int of_gpio_simple_xlate(struct gpio_chip *gc, - const struct of_phandle_args *gpiospec, - u32 *flags) +static int of_gpio_twocell_xlate(struct gpio_chip *gc, + const struct of_phandle_args *gpiospec, + u32 *flags) { /* * We're discouraging gpio_cells < 2, since that way you'll have to @@ -951,7 +951,7 @@ static int of_gpio_simple_xlate(struct gpio_chip *gc, * number and the flags from a single gpio cell -- this is possible, * but not recommended). */ - if (gc->of_gpio_n_cells < 2) { + if (gc->of_gpio_n_cells != 2) { WARN_ON(1); return -EINVAL; } @@ -968,6 +968,49 @@ static int of_gpio_simple_xlate(struct gpio_chip *gc, return gpiospec->args[0]; } +/** + * of_gpio_threecell_xlate - translate threecell gpiospec to the GPIO number and flags + * @gc: pointer to the gpio_chip structure + * @gpiospec: GPIO specifier as found in the device tree + * @flags: a flags pointer to fill in + * + * This is simple translation function, suitable for the most 1:n mapped + * GPIO chips, i.e. several GPIO chip instances from one device tree node. + * In this case the following binding is implied: + * + * foo-gpios = <&gpio instance offset flags>; + * + * Returns: + * GPIO number (>= 0) on success, negative errno on failure. + */ +static int of_gpio_threecell_xlate(struct gpio_chip *gc, + const struct of_phandle_args *gpiospec, + u32 *flags) +{ + if (gc->of_gpio_n_cells != 3) { + WARN_ON(1); + return -EINVAL; + } + + if (WARN_ON(gpiospec->args_count != 3)) + return -EINVAL; + + /* + * Check chip instance number, the driver responds with true if + * this is the chip we are looking for. + */ + if (!gc->of_node_instance_match(gc, gpiospec->args[0])) + return -EINVAL; + + if (gpiospec->args[1] >= gc->ngpio) + return -EINVAL; + + if (flags) + *flags = gpiospec->args[2]; + + return gpiospec->args[1]; +} + #if IS_ENABLED(CONFIG_OF_GPIO_MM_GPIOCHIP) #include /** @@ -1068,7 +1111,15 @@ static int of_gpiochip_add_pin_range(struct gpio_chip *chip) has_group_names = of_property_present(np, group_names_propname); for (;; index++) { - ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, + /* + * Ordinary phandles contain 2-3 cells: + * gpios = <&gpio [instance] offset flags>; + * Ranges always contain one more cell: + * gpio-ranges <&pinctrl [gpio_instance] gpio_offet pin_offet count>; + * This is why we parse chip->of_gpio_n_cells + 1 cells + */ + ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", + chip->of_gpio_n_cells + 1, index, &pinspec); if (ret) break; @@ -1078,9 +1129,25 @@ static int of_gpiochip_add_pin_range(struct gpio_chip *chip) if (!pctldev) return -EPROBE_DEFER; - offset = pinspec.args[0]; - pin = pinspec.args[1]; - count = pinspec.args[2]; + if (chip->of_gpio_n_cells == 3) { + /* First cell is the gpiochip instance number */ + offset = pinspec.args[1]; + pin = pinspec.args[2]; + count = pinspec.args[3]; + } else { + offset = pinspec.args[0]; + pin = pinspec.args[1]; + count = pinspec.args[2]; + } + + /* + * With multiple GPIO chips per node, check that this chip is the + * right instance. + */ + if (chip->of_node_instance_match && + (chip->of_gpio_n_cells == 3) && + !chip->of_node_instance_match(chip, pinspec.args[0])) + continue; /* Ignore ranges outside of this GPIO chip */ if (offset >= (chip->offset + chip->ngpio)) @@ -1170,8 +1237,14 @@ int of_gpiochip_add(struct gpio_chip *chip) return 0; if (!chip->of_xlate) { - chip->of_gpio_n_cells = 2; - chip->of_xlate = of_gpio_simple_xlate; + if (chip->of_gpio_n_cells == 3) { + if (!chip->of_node_instance_match) + return -EINVAL; + chip->of_xlate = of_gpio_threecell_xlate; + } else { + chip->of_gpio_n_cells = 2; + chip->of_xlate = of_gpio_twocell_xlate; + } } if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index 2dd7cb9cc270a68ddedbcdd5d44e0d0f88dfa785..680217ef8d92d0a6161356cc556eb5456a00eeb9 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -516,10 +516,32 @@ struct gpio_chip { /** * @of_gpio_n_cells: * - * Number of cells used to form the GPIO specifier. + * Number of cells used to form the GPIO specifier. The standard is 2 + * cells: + * + * gpios = <&gpio offset flags>; + * + * some complex GPIO controllers instantiate more than one chip per + * device tree node and have 3 cells: + * + * gpios = <&gpio instance offset flags>; + * + * Legacy GPIO controllers may even have 1 cell: + * + * gpios = <&gpio offset>; */ unsigned int of_gpio_n_cells; + /** + * of_node_instance_match: + * + * Determine if a chip is the right instance. Must be implemented by + * any driver using more than one gpio_chip per device tree node. + * Returns true if gc is the instance indicated by i (which is the + * first cell in the phandles for GPIO lines and gpio-ranges). + */ + bool (*of_node_instance_match)(struct gpio_chip *gc, unsigned int i); + /** * @of_xlate: *