All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrice CHOTARD <patrice.chotard@st.com>
To: u-boot@lists.denx.de
Subject: [PATCH v2 07/11] gpio: stmfx: add function stmfx_read_reg and stmfx_write_reg
Date: Thu, 2 Jul 2020 07:56:56 +0000	[thread overview]
Message-ID: <4e9fe3d1-9b56-4257-2a8a-9a208e0c4723@st.com> (raw)
In-Reply-To: <20200604123033.25499-5-patrick.delaunay@st.com>

Hi Patrick

On 6/4/20 2:30 PM, Patrick Delaunay wrote:
> Add the helper functions stmfx_read_reg() and stmfx_write_reg() to avoid
> duplicated code for access to stmfx's register with mask.
>
> Signed-off-by: Patrick Delaunay <patrick.delaunay@st.com>
> ---
>
> Changes in v2:
> - NEW: split previous patch [5/5] gpio: stmfx: add set_config ops
>
>  drivers/pinctrl/pinctrl-stmfx.c | 68 +++++++++++++--------------------
>  1 file changed, 26 insertions(+), 42 deletions(-)
>
> diff --git a/drivers/pinctrl/pinctrl-stmfx.c b/drivers/pinctrl/pinctrl-stmfx.c
> index 657ca2e240..5d15424b84 100644
> --- a/drivers/pinctrl/pinctrl-stmfx.c
> +++ b/drivers/pinctrl/pinctrl-stmfx.c
> @@ -74,43 +74,49 @@ static int stmfx_write(struct udevice *dev, uint offset, unsigned int val)
>  	return dm_i2c_reg_write(dev_get_parent(dev), offset, val);
>  }
>  
> -static int stmfx_conf_set_pupd(struct udevice *dev, unsigned int pin, u32 pupd)
> +static int stmfx_read_reg(struct udevice *dev, u8 reg_base, uint offset)
>  {
> -	u8 reg = STMFX_REG_GPIO_PUPD + get_reg(pin);
> -	u32 mask = get_mask(pin);
> +	u8 reg = reg_base + get_reg(offset);
> +	u32 mask = get_mask(offset);
>  	int ret;
>  
>  	ret = stmfx_read(dev, reg);
>  	if (ret < 0)
>  		return ret;
> -	ret = (ret & ~mask) | (pupd ? mask : 0);
>  
> -	return stmfx_write(dev, reg, ret);
> +	return ret < 0 ? ret : !!(ret & mask);
>  }
>  
> -static int stmfx_conf_set_type(struct udevice *dev, unsigned int pin, u32 type)
> +static int stmfx_write_reg(struct udevice *dev, u8 reg_base, uint offset,
> +			   uint val)
>  {
> -	u8 reg = STMFX_REG_GPIO_TYPE + get_reg(pin);
> -	u32 mask = get_mask(pin);
> +	u8 reg = reg_base + get_reg(offset);
> +	u32 mask = get_mask(offset);
>  	int ret;
>  
>  	ret = stmfx_read(dev, reg);
>  	if (ret < 0)
>  		return ret;
> -	ret = (ret & ~mask) | (type ? mask : 0);
> +	ret = (ret & ~mask) | (val ? mask : 0);
>  
>  	return stmfx_write(dev, reg, ret);
>  }
>  
> -static int stmfx_gpio_get(struct udevice *dev, unsigned int offset)
> +static int stmfx_conf_set_pupd(struct udevice *dev, unsigned int offset,
> +			       uint pupd)
>  {
> -	u32 reg = STMFX_REG_GPIO_STATE + get_reg(offset);
> -	u32 mask = get_mask(offset);
> -	int ret;
> +	return stmfx_write_reg(dev, STMFX_REG_GPIO_PUPD, offset, pupd);
> +}
>  
> -	ret = stmfx_read(dev, reg);
> +static int stmfx_conf_set_type(struct udevice *dev, unsigned int offset,
> +			       uint type)
> +{
> +	return stmfx_write_reg(dev, STMFX_REG_GPIO_TYPE, offset, type);
> +}
>  
> -	return ret < 0 ? ret : !!(ret & mask);
> +static int stmfx_gpio_get(struct udevice *dev, unsigned int offset)
> +{
> +	return stmfx_read_reg(dev, STMFX_REG_GPIO_STATE, offset);
>  }
>  
>  static int stmfx_gpio_set(struct udevice *dev, unsigned int offset, int value)
> @@ -123,50 +129,28 @@ static int stmfx_gpio_set(struct udevice *dev, unsigned int offset, int value)
>  
>  static int stmfx_gpio_get_function(struct udevice *dev, unsigned int offset)
>  {
> -	u32 reg = STMFX_REG_GPIO_DIR + get_reg(offset);
> -	u32 mask = get_mask(offset);
> -	int ret;
> -
> -	ret = stmfx_read(dev, reg);
> +	int ret = stmfx_read_reg(dev, STMFX_REG_GPIO_DIR, offset);
>  
>  	if (ret < 0)
>  		return ret;
>  	/* On stmfx, gpio pins direction is (0)input, (1)output. */
>  
> -	return ret & mask ? GPIOF_OUTPUT : GPIOF_INPUT;
> +	return ret ? GPIOF_OUTPUT : GPIOF_INPUT;
>  }
>  
>  static int stmfx_gpio_direction_input(struct udevice *dev, unsigned int offset)
>  {
> -	u32 reg = STMFX_REG_GPIO_DIR + get_reg(offset);
> -	u32 mask = get_mask(offset);
> -	int ret;
> -
> -	ret = stmfx_read(dev, reg);
> -	if (ret < 0)
> -		return ret;
> -
> -	ret &= ~mask;
> -
> -	return stmfx_write(dev, reg, ret & ~mask);
> +	return stmfx_write_reg(dev, STMFX_REG_GPIO_DIR, offset, 0);
>  }
>  
>  static int stmfx_gpio_direction_output(struct udevice *dev,
>  				       unsigned int offset, int value)
>  {
> -	u32 reg = STMFX_REG_GPIO_DIR + get_reg(offset);
> -	u32 mask = get_mask(offset);
> -	int ret;
> -
> -	ret = stmfx_gpio_set(dev, offset, value);
> -	if (ret < 0)
> -		return ret;
> -
> -	ret = stmfx_read(dev, reg);
> +	int ret = stmfx_gpio_set(dev, offset, value);
>  	if (ret < 0)
>  		return ret;
>  
> -	return stmfx_write(dev, reg, ret | mask);
> +	return stmfx_write_reg(dev, STMFX_REG_GPIO_DIR, offset, 1);
>  }
>  
>  static int stmfx_gpio_probe(struct udevice *dev)

Reviewed-by: Patrice Chotard <patrice.chotard@st.com>

Thanks

  reply	other threads:[~2020-07-02  7:56 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 ` [PATCH v2 04/11] gpio: stm32: add ops get_dir_flags Patrick Delaunay
2020-07-02  7:51   ` 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 [this message]
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=4e9fe3d1-9b56-4257-2a8a-9a208e0c4723@st.com \
    --to=patrice.chotard@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.