From patchwork Tue Feb 18 10:28:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 866920 Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (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 4C00B23A561 for ; Tue, 18 Feb 2025 10:28:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739874488; cv=none; b=bCzzqFn4Ukas2vO1T8g1Im4+5tEBJue1Wyod4fEUconu/wrXtkNkm1xv7ZtEZtG+Gwz9fcYiu61f6yHVriLUogVtAQOnxgYKEdGkptyWNqglZtBqM3nm6FNpPbQRx+FtmfL8JPDO4gHC0qmCyeh5Ap7lV9JJXPjSQ9iLglnJcdo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739874488; c=relaxed/simple; bh=5m7IEuqjTOk0xuSmoP9VMtnChopWqtVY7I9zYzyUWLQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=efi6AqZ+7gsF1djw1YNNo/ROUdQsfLYkqXiXLEVPIXD3l5TJNQrc1z764zs1taKZeEA2BkTcC4b34Q35Nwp7k4YbbcH3YcZayxQV6g0Ydw7TA8JhMmgehW2QUjcES2ZTR7/l28EuDwvuyETJD3BALmTVk4vewzsonYKxEqCC2nQ= 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=s+TxVqOz; arc=none smtp.client-ip=209.85.167.43 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="s+TxVqOz" Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-546210287c1so2023714e87.2 for ; Tue, 18 Feb 2025 02:28:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1739874484; x=1740479284; 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=NlYPz1W0XXxDMXL7vX3M+/OOt1iQJihziblj+Wiubyk=; b=s+TxVqOzD67SQX726yJWzXwmLwgwLgNomrVvwwaKamZg9wtcpJiXBOXa4o6M/e3uNw Zf/9XDOX2BOEtXo3goZMt/HQuEzLqyBDyqOslLFxBbZo5CH+JzdHocw4BdL0/aHb531N ZZfC7szi4sNxVNhF0Bl5pdIvNSSz2rmE1QsUIZvWzlu6lTUSZtR1U80x8CmBOjzMIYCw cb1S14chjwYfyQ+/aX3DOU28uoFvsg55texwnghjrQSLoN2pT1sNLt6U3hXf7oWUwwdP BhU9iBOoENItEp2V6cphWRhr5dj/TTYTB8R4hpsPSgzp/ht29zZF6IxR4zGpR+hiXRkI t1eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739874484; x=1740479284; 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=NlYPz1W0XXxDMXL7vX3M+/OOt1iQJihziblj+Wiubyk=; b=EsYF7HvBYQmSWlLrVM6w1BjW6ky421tuOMqfEXxHS2/Zmzxc8civ5nFTnUG5/6uUgj HDIWtAZo9DMQG11T1DgsBFzEdByHY4FEG3AMYwe6nDHd7uYsL7FCDiaCSbwfaqZhNdJq g7RcgSZ2FVRzwWiuBGg2NZsQp1z0ylSUTBUbYmsrDK2gYiPoA7EUxYND1w728QNTSfQo 4HWyIziKctkgbwDOMvFfN+ZzwmsVlZAiFhkm+2RZ8gJ6zb9VqMEkRIU8wpKGaF0omC/P q/tibvgxOxl8QPvGMzureRPOu3/lhWsAtieTTHg4kHIv3aG0iBT+AOxFOVrkC5IJtZSl cSrg== X-Forwarded-Encrypted: i=1; AJvYcCWaH+c+c+HTRD5H0iVm5qwGxp/F5J5F389SDzUQCmB01N4DhKjtvoAmeOgLWRsqnaVb/NIqGlR/2jdG@vger.kernel.org X-Gm-Message-State: AOJu0Yzk884N93+9CFm1g0v8NSl6rBZ6gPCzpFo/6YFlejLw4A/P+q6j H2D3kdkIdXLCii93Fr7ebDdxe6G9Y19oS5K9gIrYDlE5B90g89uicv/ApeBgOcI= X-Gm-Gg: ASbGncvOkTnUi8Y5DALUGiGCaBRdyiyXuUxk0anipFNKgWDVVHDBhiVbidlENmxASWH cmgcwY+6ae1UPrELzI0SC/CVL4raaiVZsXpbY2vZPA+Kg2q5xdNtKtOOXpn/P9vXVL8cFn5Uu7I zlWdgEYuM65BhRSOuXI/4ZlwHcHuq9E5+Rn0aBEEEABuabEe4KsvFeEbKXQ5erj9m3COqT3ltWW ieuIWUJjznC+xxUcQnvaCQa/9NN22DWu7FAULWOs5hgxGxvus7vQFKo6xL0h722QuOwis8nw3C/ Hwlc1RO8DapLP/9QdzDvi3wKZw== X-Google-Smtp-Source: AGHT+IFwtsLM/+sS4eETwVp/OBL4ztcHeKB6TjRIA9MjXPdkOuFYUIl7utDiOa0003NcetAuCTQd5Q== X-Received: by 2002:a05:6512:318c:b0:545:2f9f:5f6a with SMTP id 2adb3069b0e04-5452fe3a89cmr3733546e87.14.1739874484339; Tue, 18 Feb 2025 02:28:04 -0800 (PST) Received: from [192.168.1.140] ([85.235.12.238]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5461e202368sm830756e87.61.2025.02.18.02.28.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Feb 2025 02:28:03 -0800 (PST) From: Linus Walleij Date: Tue, 18 Feb 2025 11:28:01 +0100 Subject: [PATCH RTF 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: <20250218-gpio-ranges-fourcell-v1-1-b1f3db6c8036@linaro.org> References: <20250218-gpio-ranges-fourcell-v1-0-b1f3db6c8036@linaro.org> In-Reply-To: <20250218-gpio-ranges-fourcell-v1-0-b1f3db6c8036@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 X-Mailer: b4 0.14.2 Instead of modifying the contents of the array of valued read in from a phandle, use local variables to store the values. This makes the code easier to read and the array immutable. 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 18 10:28:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 866276 Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) (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 1938A23959F for ; Tue, 18 Feb 2025 10:28:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739874490; cv=none; b=UWDEKrioVxdmXvuFgcd9Bz3oJpPdLvjSPlI21JtGDayRubqFkHcTQlRq9W4Yx7miRhQ3h26Tx5mB5r3VhjQRXfibHl+FP9IpTtQGZmMgojkoBSJErMjmNzyNgQwpVfw2KNVUL0U+/7P1DOaiEn1h35Lrt8FZ+Ego0A6KBvNLqgU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739874490; c=relaxed/simple; bh=dZLGpdNd1A4j3GPoXQ09AaawNXIpE5erJf2/7ZjsIVo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LM24y2w/XtCxMCAJsoABApuz8vU0WLaBG+kJjBp9m+y0qnh9kUov93S9INGWfX/Lwk+xh7IIXzzkMNAh0h+iT4Vm59pWgp2GMIXMYrMLq+NEL4EPmpQler8wKQ4G4Sh9AjLU+IlQKvkrQfthkIE/CMS71YL6ETgGvAs9IzLN9h8= 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=ac6ZqgOZ; arc=none smtp.client-ip=209.85.167.47 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="ac6ZqgOZ" Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-54529e15643so3391991e87.1 for ; Tue, 18 Feb 2025 02:28:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1739874486; x=1740479286; 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=wk2RAfTMKuDExTvoi9nEepL1tvSfB7+G20+tDZme8pI=; b=ac6ZqgOZ3ivEW+76N/V9TH9CIBYlM8qG0p6HW3hKWLu6h45n1owaOepMBnjKuILHJ0 wzQyswjvwHiYiiQX6OzO3nCdlyL/vgSrSqJI8GD0q4S90RDG2KeVZUu/ejWbkQHSmote JgwalLomY5HVI1l6UwN1o8GK8p1j1QgZ9oCFt9eVLWf/xjayu99o0giaGwgetX8Q2Clb h6U9s0Og5zG8ZqTiL0HGoDxHx7J+mMsqiWffpzp4q8lZ81SrpKaz6A6lQa4lXdAIgeCm BUJj7G2L0/tel3aVgprUo6GiEkn2frFWWvpHOTsMMyYpVXKxiknaEfQxvSdiWjC8GIZW SRSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739874486; x=1740479286; 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=wk2RAfTMKuDExTvoi9nEepL1tvSfB7+G20+tDZme8pI=; b=p72iz0sinC901/G09kjUE4frL8KcbRB/cMdAOCcguRbTlh6SQYNF2aRo+HkGGKlwPb 0vVBalYPqMcVS534OBvw8Rnh/cYBUNN+pGiSERsUiRzn5phYrztRwY5y6JijkODJmpif d7uP+mWFz7UwPsEapv74yJXV/nUMPYcVI8lc663o2tUKvzj5GX+0ANDLSKY87m1Uy4nJ hXhM8LLXAGKz4Jb+MPoaHaUEXm6xtSTcT/97Kf64WLBr5AE4kQBTsGdXg6CZAtcrHz52 Nz1cDBdothGoZAwHKEbQ2uvrt914x3K13r5M7eMMj+pXjxCCMGBUseMkdsFuPp1BGhBO Yxmg== X-Forwarded-Encrypted: i=1; AJvYcCXyNNHb+3Ir8NxC8LBhhNPRkl4uXBHgjaaWEcujID+e2wfGbMAP8c51WQSRI6FrCvGDa+AhJfUvbZAd@vger.kernel.org X-Gm-Message-State: AOJu0YxGCxZzqX/wIodox4UiwLk0hUK1GDPSAp5ojJN3LTQmWxTB/vB1 /M6uf8tL3ONJ0rIOBQnMwdXXrZYH9N/kJbPtMIkzXEyaoeQLJlO3Tq65pyo3nJQ= X-Gm-Gg: ASbGncu3YR+3ljU8hqo7/iJicdAE0gg3Tk6qaadt67NwQAj8w1tdtL4E9naEUbHIPx6 Z8F8pNsQNSB6J1QxVFG6VjUBOK21LxI5AqUqd6Jb3KBIcwImOTMSV+/kEboegzekVusOyxQCnmf 0Un3yRRCJR5vnmJJsr/Bu6IqX9CaxFjU0lO3ermgd+oFNZwt1lfEV1KZXd7a3LwGercZQ0fUZcN FL4wJeRJqSgWxtYC42XcHIKw8Tnfb+jTiHAhFrLHsimuU5aApQUZqyH41TI/N5x3jlz0DUO4xLa mbUZa2fox0+YMprOLqCHZcM8Uw== X-Google-Smtp-Source: AGHT+IFwCQ976IhxBLO/1FXtKhY2VNxzqS2VEuXwm1jzlWawCytrp71miBBjM8shP2zZEQWc8AfpPw== X-Received: by 2002:a05:6512:2346:b0:545:1049:eb4f with SMTP id 2adb3069b0e04-545302cee81mr3547677e87.0.1739874486095; Tue, 18 Feb 2025 02:28:06 -0800 (PST) Received: from [192.168.1.140] ([85.235.12.238]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5461e202368sm830756e87.61.2025.02.18.02.28.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Feb 2025 02:28:04 -0800 (PST) From: Linus Walleij Date: Tue, 18 Feb 2025 11:28:02 +0100 Subject: [PATCH RTF 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: <20250218-gpio-ranges-fourcell-v1-2-b1f3db6c8036@linaro.org> References: <20250218-gpio-ranges-fourcell-v1-0-b1f3db6c8036@linaro.org> In-Reply-To: <20250218-gpio-ranges-fourcell-v1-0-b1f3db6c8036@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 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>; Signed-off-by: Linus Walleij --- drivers/gpio/gpiolib-of.c | 93 ++++++++++++++++++++++++++++++++++++++++----- include/linux/gpio/driver.h | 24 +++++++++++- 2 files changed, 106 insertions(+), 11 deletions(-) diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c index 86405218f4e2ddc951a1a9d168e886400652bf60..614590a5bcd10e5605ecb66ebd956250e4ea1fd8 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 @@ -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..70a361f6aba06d4a11e5ca913ec79411d7a11b3c 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 i 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: *