All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Delaunay <patrick.delaunay@st.com>
To: u-boot@lists.denx.de
Subject: [PATCH v2 04/11] gpio: stm32: add ops get_dir_flags
Date: Thu, 4 Jun 2020 14:30:26 +0200	[thread overview]
Message-ID: <20200604143022.v2.4.I10d17ebccc888338f2b9f23a803aee50c0f8e089@changeid> (raw)
In-Reply-To: <20200604123033.25499-1-patrick.delaunay@st.com>

Add ops get_dir_flags() to read dir flags from GPIO registers.

Signed-off-by: Patrick Delaunay <patrick.delaunay@st.com>
---

Changes in v2: None

 drivers/gpio/stm32_gpio.c | 59 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/drivers/gpio/stm32_gpio.c b/drivers/gpio/stm32_gpio.c
index f120ee9808..5bff27f75b 100644
--- a/drivers/gpio/stm32_gpio.c
+++ b/drivers/gpio/stm32_gpio.c
@@ -41,6 +41,11 @@ static void stm32_gpio_set_moder(struct stm32_gpio_regs *regs,
 	clrsetbits_le32(&regs->moder, mask, mode << bits_index);
 }
 
+static int stm32_gpio_get_moder(struct stm32_gpio_regs *regs, int idx)
+{
+	return (readl(&regs->moder) >> MODE_BITS(idx)) & MODE_BITS_MASK;
+}
+
 static void stm32_gpio_set_otype(struct stm32_gpio_regs *regs,
 				 int idx,
 				 enum stm32_gpio_otype otype)
@@ -51,6 +56,12 @@ static void stm32_gpio_set_otype(struct stm32_gpio_regs *regs,
 	clrsetbits_le32(&regs->otyper, OTYPE_MSK << bits, otype << bits);
 }
 
+static enum stm32_gpio_otype stm32_gpio_get_otype(struct stm32_gpio_regs *regs,
+						  int idx)
+{
+	return (readl(&regs->otyper) >> OTYPE_BITS(idx)) & OTYPE_MSK;
+}
+
 static void stm32_gpio_set_pupd(struct stm32_gpio_regs *regs,
 				int idx,
 				enum stm32_gpio_pupd pupd)
@@ -61,6 +72,12 @@ static void stm32_gpio_set_pupd(struct stm32_gpio_regs *regs,
 	clrsetbits_le32(&regs->pupdr, PUPD_MASK << bits, pupd << bits);
 }
 
+static enum stm32_gpio_pupd stm32_gpio_get_pupd(struct stm32_gpio_regs *regs,
+						int idx)
+{
+	return (readl(&regs->pupdr) >> PUPD_BITS(idx)) & PUPD_MASK;
+}
+
 /*
  * convert gpio offset to gpio index taking into account gpio holes
  * into gpio bank
@@ -202,6 +219,47 @@ static int stm32_gpio_set_dir_flags(struct udevice *dev, unsigned int offset,
 	return 0;
 }
 
+static int stm32_gpio_get_dir_flags(struct udevice *dev, unsigned int offset,
+				    ulong *flags)
+{
+	struct stm32_gpio_priv *priv = dev_get_priv(dev);
+	struct stm32_gpio_regs *regs = priv->regs;
+	int idx;
+	ulong dir_flags = 0;
+
+	idx = stm32_offset_to_index(dev, offset);
+	if (idx < 0)
+		return idx;
+
+	switch (stm32_gpio_get_moder(regs, idx)) {
+	case STM32_GPIO_MODE_OUT:
+		dir_flags |= GPIOD_IS_OUT;
+		if (stm32_gpio_get_otype(regs, idx) == STM32_GPIO_OTYPE_OD)
+			dir_flags |= GPIOD_OPEN_DRAIN;
+		if (readl(&regs->idr) & BIT(idx))
+			dir_flags |= GPIOD_IS_OUT_ACTIVE;
+		break;
+	case STM32_GPIO_MODE_IN:
+		dir_flags |= GPIOD_IS_IN;
+		switch (stm32_gpio_get_pupd(regs, idx)) {
+		case STM32_GPIO_PUPD_UP:
+			dir_flags |= GPIOD_PULL_UP;
+			break;
+		case STM32_GPIO_PUPD_DOWN:
+			dir_flags |= GPIOD_PULL_DOWN;
+			break;
+		default:
+			break;
+		}
+		break;
+	default:
+		break;
+	}
+	*flags = dir_flags;
+
+	return 0;
+}
+
 static const struct dm_gpio_ops gpio_stm32_ops = {
 	.direction_input	= stm32_gpio_direction_input,
 	.direction_output	= stm32_gpio_direction_output,
@@ -209,6 +267,7 @@ static const struct dm_gpio_ops gpio_stm32_ops = {
 	.set_value		= stm32_gpio_set_value,
 	.get_function		= stm32_gpio_get_function,
 	.set_dir_flags		= stm32_gpio_set_dir_flags,
+	.get_dir_flags		= stm32_gpio_get_dir_flags,
 };
 
 static int gpio_stm32_probe(struct udevice *dev)
-- 
2.17.1

  parent reply	other threads:[~2020-06-04 12:30 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-04 12:30 [PATCH v2 00/11] stm32mp1: activate gpio hog support and add new pinctrl ops Patrick Delaunay
2020-06-04 12:30 ` [PATCH v2 01/11] configs: stm32mp1: activate CONFIG_GPIO_HOG Patrick Delaunay
2020-07-02  7:41   ` [Uboot-stm32] " Patrice CHOTARD
2020-06-04 12:30 ` [PATCH v2 02/11] board: stm32mp1: update the gpio hog support Patrick Delaunay
2020-07-02  7:42   ` [Uboot-stm32] " Patrice CHOTARD
2020-06-04 12:30 ` [PATCH v2 03/11] gpio: stm32: add ops set_dir_flags Patrick Delaunay
2020-07-02  7:48   ` Patrice CHOTARD
2020-06-04 12:30 ` Patrick Delaunay [this message]
2020-07-02  7:51   ` [PATCH v2 04/11] gpio: stm32: add ops get_dir_flags Patrice CHOTARD
2020-06-04 12:30 ` [PATCH v2 05/11] gpio: stmfx: move function to prepare new ops introduction Patrick Delaunay
2020-07-02  7:51   ` Patrice CHOTARD
2020-06-04 12:30 ` [PATCH v2 06/11] gpio: stmfx: rename function used to change pin configuration Patrick Delaunay
2020-07-02  7:51   ` Patrice CHOTARD
2020-06-04 12:30 ` [PATCH v2 07/11] gpio: stmfx: add function stmfx_read_reg and stmfx_write_reg Patrick Delaunay
2020-07-02  7:56   ` Patrice CHOTARD
2020-06-04 12:30 ` [PATCH v2 08/11] gpio: stmfx: add ops set_dir_flag Patrick Delaunay
2020-07-02  7:58   ` Patrice CHOTARD
2020-06-04 12:30 ` [PATCH v2 09/11] gpio: stmfx: add ops get_dir_flags Patrick Delaunay
2020-07-02  7:58   ` Patrice CHOTARD
2020-06-04 12:30 ` [PATCH v2 10/11] pinctrl: stmfx: add information on pin configuration Patrick Delaunay
2020-07-02  7:59   ` Patrice CHOTARD
2020-06-04 12:30 ` [PATCH v2 11/11] pinctrl: stm32: " Patrick Delaunay
2020-07-02  8:02   ` Patrice CHOTARD

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200604143022.v2.4.I10d17ebccc888338f2b9f23a803aee50c0f8e089@changeid \
    --to=patrick.delaunay@st.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.