From: "Grygorii.Strashko-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org" <grygorii.strashko-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
To: Alexander Sverdlin
<alexander.sverdlin-xNZwKgViW5gAvxtiuMwx3w@public.gmane.org>,
linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
Wolfram Sang <wsa-z923LK4zBo2bacvFa/9K2g@public.gmane.org>,
Kevin Hilman
<khilman-1D3HCaltpLuhEniVeURVKkEOCMrvLtNR@public.gmane.org>,
Sekhar Nori <nsekhar-l0cyMroinI0@public.gmane.org>,
"Karicheri,
Muralidharan" <m-karicheri2-l0cyMroinI0@public.gmane.org>,
"Vishwanathrao Badarkhe,
Manish" <manishv.b-l0cyMroinI0@public.gmane.org>Murali Karicheri
<m-karicheri2-l0cyMroinI0@public.gmane.org>
Cc: Lawnick Michael 61283229
<michael.lawnick-OYasijW0DpE@public.gmane.org>,
Mike Looijmans <mike.looijmans-Oq418RWZeHk@public.gmane.org>,
Mastalski Bartosz
<bartosz.mastalski-xNZwKgViW5gAvxtiuMwx3w@public.gmane.org>
Subject: Re: [PATCH 2/3] i2c: davinci: Refactor i2c_davinci_wait_bus_not_busy()
Date: Wed, 11 Mar 2015 20:35:03 +0200 [thread overview]
Message-ID: <55008AD7.4070905@linaro.org> (raw)
In-Reply-To: <55003E6B.40008-xNZwKgViW5gAvxtiuMwx3w@public.gmane.org>
Hi Alexander,
On 03/11/2015 03:08 PM, Alexander Sverdlin wrote:
> There are several problems in the function:
> - "to_cnt" variable does nothing
> - schedule_timeout() call without setting current state does nothing
> - "allow_sleep" parameter is not really used
>
> Refactor the function so that it really tries to wait. In case of timeout try
> to recover the bus and finally initialize the controller. We cannot really
> do more than that, so it makes no sense to check BB bit after init.
>
> Signed-off-by: Alexander Sverdlin <alexander.sverdlin-xNZwKgViW5gAvxtiuMwx3w@public.gmane.org>
> ---
> drivers/i2c/busses/i2c-davinci.c | 53 +++++++++++++++++++++----------------
> 1 files changed, 30 insertions(+), 23 deletions(-)
>
> diff --git a/drivers/i2c/busses/i2c-davinci.c b/drivers/i2c/busses/i2c-davinci.c
> index 98759ae..ca22479 100644
> --- a/drivers/i2c/busses/i2c-davinci.c
> +++ b/drivers/i2c/busses/i2c-davinci.c
> @@ -152,6 +152,26 @@ static void davinci_i2c_clock_pulse(unsigned int scl_pin)
> }
> }
>
> +/*
> + * Wait until specific bit in status register has particular value
> + * Function returns 0 if condition was met,
> + * -ETIMEDOUT in case of timeout.
> + */
> +static int i2c_davinci_wait_status_change(struct davinci_i2c_dev *dev, u16 mask,
> + u16 val)
do we really need it as separate function? (even if it looks good:)
> +{
> + unsigned long timeout = jiffies + dev->adapter.timeout;
> +
> + while ((davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG) & mask) != val) {
> + if (time_after(jiffies, timeout))
> + return -ETIMEDOUT;
> + set_current_state(TASK_UNINTERRUPTIBLE);
> + schedule_timeout(1);
schedule_timeout_uninterruptible()
> + }
> +
> + return 0;
> +}
> +
> /* This routine does i2c bus recovery as specified in the
> * i2c protocol Rev. 03 section 3.16 titled "Bus clear"
> */
> @@ -269,29 +289,16 @@ static int i2c_davinci_init(struct davinci_i2c_dev *dev)
> /*
> * Waiting for bus not busy
> */
> -static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev,
> - char allow_sleep)
> +static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev)
> {
> - unsigned long timeout;
> - static u16 to_cnt;
> -
> - timeout = jiffies + dev->adapter.timeout;
> - while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG)
> - & DAVINCI_I2C_STR_BB) {
> - if (to_cnt <= DAVINCI_I2C_MAX_TRIES) {
> - if (time_after(jiffies, timeout)) {
> - dev_warn(dev->dev,
> - "timeout waiting for bus ready\n");
> - to_cnt++;
> - return -ETIMEDOUT;
> - } else {
> - to_cnt = 0;
> - davinci_i2c_recover_bus(dev);
> - i2c_davinci_init(dev);
> - }
> - }
> - if (allow_sleep)
> - schedule_timeout(1);
> + if (i2c_davinci_wait_status_change(dev, DAVINCI_I2C_STR_BB, 0)) {
> + dev_warn(dev->dev, "timeout waiting for bus ready\n");
> + davinci_i2c_recover_bus(dev);
> + i2c_davinci_init(dev);
> + /*
> + * the bus should not be busy after init, otherwise something
> + * is badly broken
> + */
I think you should recheck BB and return error if it's still detected.
> }
>
> return 0;
> @@ -449,7 +456,7 @@ i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
>
> dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
>
> - ret = i2c_davinci_wait_bus_not_busy(dev, 1);
> + ret = i2c_davinci_wait_bus_not_busy(dev);
> if (ret < 0) {
> dev_warn(dev->dev, "timeout waiting for bus ready\n");
> return ret;
>
regards,
-grygorii
--
regards,
-grygorii
next prev parent reply other threads:[~2015-03-11 18:35 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-03-11 13:08 [PATCH 2/3] i2c: davinci: Refactor i2c_davinci_wait_bus_not_busy() Alexander Sverdlin
[not found] ` <55003E6B.40008-xNZwKgViW5gAvxtiuMwx3w@public.gmane.org>
2015-03-11 18:35 ` Grygorii.Strashko-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org [this message]
2015-03-11 19:22 ` Alexander Sverdlin
[not found] ` <55008AD7.4070905-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2015-03-12 8:50 ` Alexander Sverdlin
[not found] ` <55015351.7040508-xNZwKgViW5gAvxtiuMwx3w@public.gmane.org>
2015-03-12 12:37 ` Grygorii Strashko
[not found] ` <CAO5CWPJWhpGCb4NvXnJJ00-9kOvvqtxQsSQbTMxODNC4tjtWzQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-03-13 7:54 ` Alexander Sverdlin
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=55008AD7.4070905@linaro.org \
--to=grygorii.strashko-qsej5fyqhm4dnm+yrofe0a@public.gmane.org \
--cc=alexander.sverdlin-xNZwKgViW5gAvxtiuMwx3w@public.gmane.org \
--cc=bartosz.mastalski-xNZwKgViW5gAvxtiuMwx3w@public.gmane.org \
--cc=khilman-1D3HCaltpLuhEniVeURVKkEOCMrvLtNR@public.gmane.org \
--cc=linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=m-karicheri2-l0cyMroinI0@public.gmane.org \
--cc=manishv.b-l0cyMroinI0@public.gmane.org \
--cc=michael.lawnick-OYasijW0DpE@public.gmane.org \
--cc=mike.looijmans-Oq418RWZeHk@public.gmane.org \
--cc=nsekhar-l0cyMroinI0@public.gmane.org \
--cc=wsa-z923LK4zBo2bacvFa/9K2g@public.gmane.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).