linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Maxime Ripard <maxime.ripard@free-electrons.com>
To: Jonathan Liu <net147@gmail.com>
Cc: David Airlie <airlied@linux.ie>, Chen-Yu Tsai <wens@csie.org>,
	linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
	linux-arm-kernel@lists.infradead.org,
	linux-sunxi@googlegroups.com
Subject: Re: [PATCH v5] drm/sun4i: hdmi: Implement I2C adapter for A10s DDC bus
Date: Wed, 28 Jun 2017 11:20:24 +0200	[thread overview]
Message-ID: <20170628092024.ejxy6itqj3hx6yew@flea> (raw)
In-Reply-To: <20170627143652.13075-1-net147@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2536 bytes --]

On Wed, Jun 28, 2017 at 12:36:52AM +1000, Jonathan Liu wrote:
> +#define SUN4I_HDMI_DDC_INT_STATUS_ERROR_MASK ( \
> +	SUN4I_HDMI_DDC_INT_STATUS_ILLEGAL_FIFO_OPERATION | \
> +	SUN4I_HDMI_DDC_INT_STATUS_DDC_RX_FIFO_UNDERFLOW | \
> +	SUN4I_HDMI_DDC_INT_STATUS_DDC_TX_FIFO_OVERFLOW | \
> +	SUN4I_HDMI_DDC_INT_STATUS_ARBITRATION_ERROR | \
> +	SUN4I_HDMI_DDC_INT_STATUS_ACK_ERROR | \
> +	SUN4I_HDMI_DDC_INT_STATUS_BUS_ERROR \
> +)
> +
> +static bool is_err_status(u32 int_status)
> +{
> +	return !!(int_status & SUN4I_HDMI_DDC_INT_STATUS_ERROR_MASK);
> +}
> +
> +static bool is_fifo_flag_unset(struct sun4i_hdmi *hdmi, u32 *fifo_status,
> +			       u32 flag)
> +{
> +	*fifo_status = readl(hdmi->base + SUN4I_HDMI_DDC_FIFO_STATUS_REG);
> +	return !(*fifo_status & flag);
> +}
> +
> +static int fifo_transfer(struct sun4i_hdmi *hdmi, u8 *buf, int len, bool read)
> +{
> +	/* 1 byte takes 9 clock cycles (8 bits + 1 ACK) */
> +	unsigned long byte_time = DIV_ROUND_UP(USEC_PER_SEC,
> +					       clk_get_rate(hdmi->ddc_clk)) * 9;

There's no real need for it to be dynamic. The clock rate will not
change, and the order of magnitude is roughly 100us, so let's just use
that (and make a comment).

> +	u32 int_status;
> +	u32 fifo_status;
> +	/* Read needs empty flag unset, write needs full flag unset */
> +	u32 flag = read ? SUN4I_HDMI_DDC_FIFO_STATUS_EMPTY :
> +			  SUN4I_HDMI_DDC_FIFO_STATUS_FULL;
> +	int ret;
> +
> +	/* Wait until error or FIFO ready */
> +	ret = readl_poll_timeout(hdmi->base + SUN4I_HDMI_DDC_INT_STATUS_REG,
> +				 int_status,
> +				 is_err_status(int_status) ||
> +				 is_fifo_flag_unset(hdmi, &fifo_status, flag),
> +				 min(len, SUN4I_HDMI_DDC_FIFO_SIZE) * byte_time,
> +				 100000);
> +
> +	if (is_err_status(int_status))
> +		return -EIO;
> +	if (ret)
> +		return -ETIMEDOUT;

Why not just have
ret = readl_poll_timeout(hdmi->base + SUN4I_HDMI_DDC_FIFO_STATUS_REG, reg,
			 !(reg & flag), 100, 100000);

if (ret < 0)
	if (is_err_status())
		return -EIO;
	return ret;


> +
> +	/* Read FIFO level */
> +	int level = (int)(fifo_status & SUN4I_HDMI_DDC_FIFO_STATUS_LEVEL_MASK);

and explicitly read the fifo status here. That will make you remove
that function that does two things while claiming that it does only
one, and it will be more obvious.

You can also just use reg at this point, instead of reading it once
again.

Maxime

-- 
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 801 bytes --]

  reply	other threads:[~2017-06-28  9:21 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-27 14:36 [PATCH v5] drm/sun4i: hdmi: Implement I2C adapter for A10s DDC bus Jonathan Liu
2017-06-28  9:20 ` Maxime Ripard [this message]
2017-06-28 10:39   ` Jonathan Liu
2017-06-29 15:56     ` Maxime Ripard
2017-06-29 22:22       ` Jonathan Liu
2017-06-30  3:16         ` Chen-Yu Tsai
2017-06-30 14:14           ` Jonathan Liu
2017-06-30 16:01             ` Maxime Ripard
2017-06-30  9:34         ` Maxime Ripard
2017-06-30  9:58           ` Jonathan Liu
2017-07-01  6:29             ` Jonathan Liu
2017-06-28 22:06 ` kbuild test robot

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=20170628092024.ejxy6itqj3hx6yew@flea \
    --to=maxime.ripard@free-electrons.com \
    --cc=airlied@linux.ie \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sunxi@googlegroups.com \
    --cc=net147@gmail.com \
    --cc=wens@csie.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).