diff mbox

[1/3] regulator: twl: Make sure we have access to powerbus before trying to write to it

Message ID 1458980895-10240-2-git-send-email-ivo.g.dimitrov.75@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Ivaylo Dimitrov March 26, 2016, 8:28 a.m. UTC
According to the TRM, we need to enable i2c access to powerbus before
writing to it. Also, a new write to powerbus should not be attempted if
there is a pending transfer. The current code does not implement that
functionality and while there are no known problems caused by that, it is
better to follow what TRM says.

Signed-off-by: Ivaylo Dimitrov <ivo.g.dimitrov.75@gmail.com>
---
 drivers/regulator/twl-regulator.c | 78 +++++++++++++++++++++++++++++++++++----
 1 file changed, 70 insertions(+), 8 deletions(-)

Comments

Ivaylo Dimitrov April 5, 2016, 10:26 p.m. UTC | #1
On 26.03.2016 10:28, Ivaylo Dimitrov wrote:
> According to the TRM, we need to enable i2c access to powerbus before
> writing to it. Also, a new write to powerbus should not be attempted if
> there is a pending transfer. The current code does not implement that
> functionality and while there are no known problems caused by that, it is
> better to follow what TRM says.
>
> Signed-off-by: Ivaylo Dimitrov <ivo.g.dimitrov.75@gmail.com>
> ---
>   drivers/regulator/twl-regulator.c | 78 +++++++++++++++++++++++++++++++++++----
>   1 file changed, 70 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/regulator/twl-regulator.c b/drivers/regulator/twl-regulator.c
> index 955a6fb..aad748b0 100644
> --- a/drivers/regulator/twl-regulator.c
> +++ b/drivers/regulator/twl-regulator.c
> @@ -21,7 +21,7 @@
>   #include <linux/regulator/machine.h>
>   #include <linux/regulator/of_regulator.h>
>   #include <linux/i2c/twl.h>
> -
> +#include <linux/delay.h>
>
>   /*
>    * The TWL4030/TW5030/TPS659x0/TWL6030 family chips include power management, a
> @@ -188,6 +188,74 @@ static int twl6030reg_is_enabled(struct regulator_dev *rdev)
>   	return grp && (val == TWL6030_CFG_STATE_ON);
>   }
>
> +#define PB_I2C_BUSY	BIT(0)
> +#define PB_I2C_BWEN	BIT(1)
> +
> +/* Wait until buffer empty/ready to send a word on power bus. */
> +static int twl4030_wait_pb_ready(void)
> +{
> +
> +	int	ret;
> +	int	timeout = 10;
> +	u8	val;
> +
> +	do {
> +		ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
> +				      TWL4030_PM_MASTER_PB_CFG);
> +		if (ret < 0)
> +			return ret;
> +
> +		if (!(val & PB_I2C_BUSY))
> +			return 0;
> +
> +		mdelay(1);
> +		timeout--;
> +	} while (timeout);
> +
> +	return -ETIMEDOUT;
> +}
> +
> +/* Send a word over the powerbus */
> +static int twl4030_send_pb_msg(unsigned msg)
> +{
> +	u8	val;
> +	int	ret;
> +
> +	/* save powerbus configuration */
> +	ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
> +			      TWL4030_PM_MASTER_PB_CFG);
> +	if (ret < 0)
> +		return ret;
> +
> +	/* Enable i2c access to powerbus */
> +	ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val | PB_I2C_BWEN,
> +			       TWL4030_PM_MASTER_PB_CFG);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = twl4030_wait_pb_ready();
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, msg >> 8,
> +			       TWL4030_PM_MASTER_PB_WORD_MSB);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, msg & 0xff,
> +			       TWL4030_PM_MASTER_PB_WORD_LSB);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = twl4030_wait_pb_ready();
> +	if (ret < 0)
> +		return ret;
> +
> +	/* Restore powerbus configuration */
> +	return twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val,
> +				TWL_MODULE_PM_MASTER);

Hmm, I made a copy/paste error here, this should be:

	return twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val,
				TWL4030_PM_MASTER_PB_CFG);

Will send a new version of the patch.

> +}
> +
>   static int twl4030reg_enable(struct regulator_dev *rdev)
>   {
>   	struct twlreg_info	*info = rdev_get_drvdata(rdev);
> @@ -324,13 +392,7 @@ static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
>   	if (!(status & (P3_GRP_4030 | P2_GRP_4030 | P1_GRP_4030)))
>   		return -EACCES;
>
> -	status = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
> -			message >> 8, TWL4030_PM_MASTER_PB_WORD_MSB);
> -	if (status < 0)
> -		return status;
> -
> -	return twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
> -			message & 0xff, TWL4030_PM_MASTER_PB_WORD_LSB);
> +	return twl4030_send_pb_msg(message);
>   }
>
>   static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
>

Ivo
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Pavel Machek April 24, 2016, 4:10 p.m. UTC | #2
Hi!

> According to the TRM, we need to enable i2c access to powerbus before
> writing to it. Also, a new write to powerbus should not be attempted if
> there is a pending transfer. The current code does not implement that
> functionality and while there are no known problems caused by that, it is
> better to follow what TRM says.
> 
> Signed-off-by: Ivaylo Dimitrov <ivo.g.dimitrov.75@gmail.com>
> ---
>  drivers/regulator/twl-regulator.c | 78 +++++++++++++++++++++++++++++++++++----
>  1 file changed, 70 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/regulator/twl-regulator.c b/drivers/regulator/twl-regulator.c
> index 955a6fb..aad748b0 100644
> --- a/drivers/regulator/twl-regulator.c
> +++ b/drivers/regulator/twl-regulator.c
> @@ -21,7 +21,7 @@
>  #include <linux/regulator/machine.h>
>  #include <linux/regulator/of_regulator.h>
>  #include <linux/i2c/twl.h>
> -
> +#include <linux/delay.h>
>  
>  /*
>   * The TWL4030/TW5030/TPS659x0/TWL6030 family chips include power management, a
> @@ -188,6 +188,74 @@ static int twl6030reg_is_enabled(struct regulator_dev *rdev)
>  	return grp && (val == TWL6030_CFG_STATE_ON);
>  }
>  
> +#define PB_I2C_BUSY	BIT(0)
> +#define PB_I2C_BWEN	BIT(1)
> +
> +/* Wait until buffer empty/ready to send a word on power bus. */
> +static int twl4030_wait_pb_ready(void)
> +{
> +
> +	int	ret;
> +	int	timeout = 10;
> +	u8	val;
> +

Can we do this plain

    while (timeout--) {
    }...

? Also... if the bit is not immediately available, it will wait for
1msec. Would it make sense to have timeout = 1000 but wait only 10usec
each time or something?

Thanks,
								Pavel
								
> +	do {
> +		ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
> +				      TWL4030_PM_MASTER_PB_CFG);
> +		if (ret < 0)
> +			return ret;
> +
> +		if (!(val & PB_I2C_BUSY))
> +			return 0;
> +
> +		mdelay(1);
> +		timeout--;
> +	} while (timeout);
> +
> +	return -ETIMEDOUT;
> +}
Ivaylo Dimitrov April 24, 2016, 5:14 p.m. UTC | #3
Hi,

On 24.04.2016 19:10, Pavel Machek wrote:
> Hi!
>
>> According to the TRM, we need to enable i2c access to powerbus before
>> writing to it. Also, a new write to powerbus should not be attempted if
>> there is a pending transfer. The current code does not implement that
>> functionality and while there are no known problems caused by that, it is
>> better to follow what TRM says.
>>
>> Signed-off-by: Ivaylo Dimitrov <ivo.g.dimitrov.75@gmail.com>
>> ---
>>   drivers/regulator/twl-regulator.c | 78 +++++++++++++++++++++++++++++++++++----
>>   1 file changed, 70 insertions(+), 8 deletions(-)
>>
>> diff --git a/drivers/regulator/twl-regulator.c b/drivers/regulator/twl-regulator.c
>> index 955a6fb..aad748b0 100644
>> --- a/drivers/regulator/twl-regulator.c
>> +++ b/drivers/regulator/twl-regulator.c
>> @@ -21,7 +21,7 @@
>>   #include <linux/regulator/machine.h>
>>   #include <linux/regulator/of_regulator.h>
>>   #include <linux/i2c/twl.h>
>> -
>> +#include <linux/delay.h>
>>
>>   /*
>>    * The TWL4030/TW5030/TPS659x0/TWL6030 family chips include power management, a
>> @@ -188,6 +188,74 @@ static int twl6030reg_is_enabled(struct regulator_dev *rdev)
>>   	return grp && (val == TWL6030_CFG_STATE_ON);
>>   }
>>
>> +#define PB_I2C_BUSY	BIT(0)
>> +#define PB_I2C_BWEN	BIT(1)
>> +
>> +/* Wait until buffer empty/ready to send a word on power bus. */
>> +static int twl4030_wait_pb_ready(void)
>> +{
>> +
>> +	int	ret;
>> +	int	timeout = 10;
>> +	u8	val;
>> +
>
> Can we do this plain
>
>      while (timeout--) {
>      }...
>

Now looking at the code, yes, while(timeout--) looks prettier, but as 
the $subject patch is in the linux-next for a couple of weeks already, 
that change will need another patch. I'll put that in my TODO, right 
after the RFC for the N900 cameras :) .

> ? Also... if the bit is not immediately available, it will wait for
> 1msec. Would it make sense to have timeout = 1000 but wait only 10usec
> each time or something?
>

Well, anyway these look like a kind of arbitrary values, I guess it will 
work both ways. But, I borrowed the code in the patch from stock Nokia 
kernel, it has been field tested on tens of thousands of devices, so, 
unless you have hard values giving a reason to do it the other way, I 
prefer to keep it as it is.

Regards,
Ivo

>> +	do {
>> +		ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
>> +				      TWL4030_PM_MASTER_PB_CFG);
>> +		if (ret < 0)
>> +			return ret;
>> +
>> +		if (!(val & PB_I2C_BUSY))
>> +			return 0;
>> +
>> +		mdelay(1);
>> +		timeout--;
>> +	} while (timeout);
>> +
>> +	return -ETIMEDOUT;
>> +}
>
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/regulator/twl-regulator.c b/drivers/regulator/twl-regulator.c
index 955a6fb..aad748b0 100644
--- a/drivers/regulator/twl-regulator.c
+++ b/drivers/regulator/twl-regulator.c
@@ -21,7 +21,7 @@ 
 #include <linux/regulator/machine.h>
 #include <linux/regulator/of_regulator.h>
 #include <linux/i2c/twl.h>
-
+#include <linux/delay.h>
 
 /*
  * The TWL4030/TW5030/TPS659x0/TWL6030 family chips include power management, a
@@ -188,6 +188,74 @@  static int twl6030reg_is_enabled(struct regulator_dev *rdev)
 	return grp && (val == TWL6030_CFG_STATE_ON);
 }
 
+#define PB_I2C_BUSY	BIT(0)
+#define PB_I2C_BWEN	BIT(1)
+
+/* Wait until buffer empty/ready to send a word on power bus. */
+static int twl4030_wait_pb_ready(void)
+{
+
+	int	ret;
+	int	timeout = 10;
+	u8	val;
+
+	do {
+		ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
+				      TWL4030_PM_MASTER_PB_CFG);
+		if (ret < 0)
+			return ret;
+
+		if (!(val & PB_I2C_BUSY))
+			return 0;
+
+		mdelay(1);
+		timeout--;
+	} while (timeout);
+
+	return -ETIMEDOUT;
+}
+
+/* Send a word over the powerbus */
+static int twl4030_send_pb_msg(unsigned msg)
+{
+	u8	val;
+	int	ret;
+
+	/* save powerbus configuration */
+	ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
+			      TWL4030_PM_MASTER_PB_CFG);
+	if (ret < 0)
+		return ret;
+
+	/* Enable i2c access to powerbus */
+	ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val | PB_I2C_BWEN,
+			       TWL4030_PM_MASTER_PB_CFG);
+	if (ret < 0)
+		return ret;
+
+	ret = twl4030_wait_pb_ready();
+	if (ret < 0)
+		return ret;
+
+	ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, msg >> 8,
+			       TWL4030_PM_MASTER_PB_WORD_MSB);
+	if (ret < 0)
+		return ret;
+
+	ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, msg & 0xff,
+			       TWL4030_PM_MASTER_PB_WORD_LSB);
+	if (ret < 0)
+		return ret;
+
+	ret = twl4030_wait_pb_ready();
+	if (ret < 0)
+		return ret;
+
+	/* Restore powerbus configuration */
+	return twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val,
+				TWL_MODULE_PM_MASTER);
+}
+
 static int twl4030reg_enable(struct regulator_dev *rdev)
 {
 	struct twlreg_info	*info = rdev_get_drvdata(rdev);
@@ -324,13 +392,7 @@  static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
 	if (!(status & (P3_GRP_4030 | P2_GRP_4030 | P1_GRP_4030)))
 		return -EACCES;
 
-	status = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
-			message >> 8, TWL4030_PM_MASTER_PB_WORD_MSB);
-	if (status < 0)
-		return status;
-
-	return twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
-			message & 0xff, TWL4030_PM_MASTER_PB_WORD_LSB);
+	return twl4030_send_pb_msg(message);
 }
 
 static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)