mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] i2c: add pmic_reg_read() for PBL use
@ 2023-12-19 16:11 Bastian Krause
  2023-12-19 16:21 ` Ahmad Fatoum
  0 siblings, 1 reply; 4+ messages in thread
From: Bastian Krause @ 2023-12-19 16:11 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum, Bastian Krause

Add a common PMIC read function to PBL which allows easy porting of
U-Boot's pmic_reg_read() in lowlevel board code.

Signed-off-by: Bastian Krause <bst@pengutronix.de>
---
 include/pbl/pmic.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/include/pbl/pmic.h b/include/pbl/pmic.h
index 0f882c5649d..bd0583370ec 100644
--- a/include/pbl/pmic.h
+++ b/include/pbl/pmic.h
@@ -9,6 +9,27 @@ struct pmic_config {
 	u8 val;
 };
 
+static void pmic_reg_read(struct pbl_i2c *i2c, int addr, u8 reg, void *buf)
+{
+	int ret;
+	struct i2c_msg msg[] = {
+		{
+			.addr = addr,
+			.buf = &reg,
+			.len = 1,
+		}, {
+			.addr = addr,
+			.flags = I2C_M_RD,
+			.buf = buf,
+			.len = 1,
+		},
+	};
+
+	ret = pbl_i2c_xfer(i2c, msg, ARRAY_SIZE(msg));
+	if (ret != ARRAY_SIZE(msg))
+		pr_err("Failed to read from pmic@%x: %d\n", addr, ret);
+}
+
 static void pmic_reg_write(struct pbl_i2c *i2c, int addr, u8 reg, u8 val)
 {
 	int ret;
-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] i2c: add pmic_reg_read() for PBL use
  2023-12-19 16:11 [PATCH] i2c: add pmic_reg_read() for PBL use Bastian Krause
@ 2023-12-19 16:21 ` Ahmad Fatoum
  2024-01-02  9:11   ` Sascha Hauer
  0 siblings, 1 reply; 4+ messages in thread
From: Ahmad Fatoum @ 2023-12-19 16:21 UTC (permalink / raw)
  To: Bastian Krause, barebox

Hello Bastian,

On 19.12.23 17:11, Bastian Krause wrote:
> Add a common PMIC read function to PBL which allows easy porting of
> U-Boot's pmic_reg_read() in lowlevel board code.
> 
> Signed-off-by: Bastian Krause <bst@pengutronix.de>
> ---
>  include/pbl/pmic.h | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
> 
> diff --git a/include/pbl/pmic.h b/include/pbl/pmic.h
> index 0f882c5649d..bd0583370ec 100644
> --- a/include/pbl/pmic.h
> +++ b/include/pbl/pmic.h
> @@ -9,6 +9,27 @@ struct pmic_config {
>  	u8 val;
>  };
>  
> +static void pmic_reg_read(struct pbl_i2c *i2c, int addr, u8 reg, void *buf)

This should be static inline to avoid warnings for boards that don't use it.

> +{
> +	int ret;
> +	struct i2c_msg msg[] = {
> +		{
> +			.addr = addr,
> +			.buf = &reg,
> +			.len = 1,
> +		}, {
> +			.addr = addr,
> +			.flags = I2C_M_RD,
> +			.buf = buf,

U-Boot has buf of type u32 *. In your barebox implementation, only the
first byte is written, so this leaves 3 bytes potentially uninitialized.

You likely want to change void *buf to u32 *val and then

  u8 buf[1];
  /* i2c_xfer... */
  *val = buf[0];

> +			.len = 1,
> +		},
> +	};
> +
> +	ret = pbl_i2c_xfer(i2c, msg, ARRAY_SIZE(msg));
> +	if (ret != ARRAY_SIZE(msg))
> +		pr_err("Failed to read from pmic@%x: %d\n", addr, ret);
> +}

Cheers,
Ahmad

> +
>  static void pmic_reg_write(struct pbl_i2c *i2c, int addr, u8 reg, u8 val)
>  {
>  	int ret;

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] i2c: add pmic_reg_read() for PBL use
  2023-12-19 16:21 ` Ahmad Fatoum
@ 2024-01-02  9:11   ` Sascha Hauer
  2024-01-02  9:13     ` Ahmad Fatoum
  0 siblings, 1 reply; 4+ messages in thread
From: Sascha Hauer @ 2024-01-02  9:11 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: Bastian Krause, barebox

On Tue, Dec 19, 2023 at 05:21:35PM +0100, Ahmad Fatoum wrote:
> Hello Bastian,
> 
> On 19.12.23 17:11, Bastian Krause wrote:
> > Add a common PMIC read function to PBL which allows easy porting of
> > U-Boot's pmic_reg_read() in lowlevel board code.
> > 
> > Signed-off-by: Bastian Krause <bst@pengutronix.de>
> > ---
> >  include/pbl/pmic.h | 21 +++++++++++++++++++++
> >  1 file changed, 21 insertions(+)
> > 
> > diff --git a/include/pbl/pmic.h b/include/pbl/pmic.h
> > index 0f882c5649d..bd0583370ec 100644
> > --- a/include/pbl/pmic.h
> > +++ b/include/pbl/pmic.h
> > @@ -9,6 +9,27 @@ struct pmic_config {
> >  	u8 val;
> >  };
> >  
> > +static void pmic_reg_read(struct pbl_i2c *i2c, int addr, u8 reg, void *buf)
> 
> This should be static inline to avoid warnings for boards that don't use it.
> 
> > +{
> > +	int ret;
> > +	struct i2c_msg msg[] = {
> > +		{
> > +			.addr = addr,
> > +			.buf = &reg,
> > +			.len = 1,
> > +		}, {
> > +			.addr = addr,
> > +			.flags = I2C_M_RD,
> > +			.buf = buf,
> 
> U-Boot has buf of type u32 *. In your barebox implementation, only the
> first byte is written, so this leaves 3 bytes potentially uninitialized.
> 
> You likely want to change void *buf to u32 *val and then
> 
>   u8 buf[1];
>   /* i2c_xfer... */
>   *val = buf[0];

When *buf is assumed to be a single byte only anyway then we can change
its type to u8 *

Sascha

> 
> > +			.len = 1,
> > +		},
> > +	};
> > +
> > +	ret = pbl_i2c_xfer(i2c, msg, ARRAY_SIZE(msg));
> > +	if (ret != ARRAY_SIZE(msg))
> > +		pr_err("Failed to read from pmic@%x: %d\n", addr, ret);
> > +}
> 
> Cheers,
> Ahmad
> 
> > +
> >  static void pmic_reg_write(struct pbl_i2c *i2c, int addr, u8 reg, u8 val)
> >  {
> >  	int ret;
> 
> -- 
> Pengutronix e.K.                           |                             |
> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] i2c: add pmic_reg_read() for PBL use
  2024-01-02  9:11   ` Sascha Hauer
@ 2024-01-02  9:13     ` Ahmad Fatoum
  0 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2024-01-02  9:13 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Bastian Krause, barebox

On 02.01.24 10:11, Sascha Hauer wrote:
> On Tue, Dec 19, 2023 at 05:21:35PM +0100, Ahmad Fatoum wrote:
>> Hello Bastian,
>>
>> On 19.12.23 17:11, Bastian Krause wrote:
>>> Add a common PMIC read function to PBL which allows easy porting of
>>> U-Boot's pmic_reg_read() in lowlevel board code.
>>>
>>> Signed-off-by: Bastian Krause <bst@pengutronix.de>
>>> ---
>>>  include/pbl/pmic.h | 21 +++++++++++++++++++++
>>>  1 file changed, 21 insertions(+)
>>>
>>> diff --git a/include/pbl/pmic.h b/include/pbl/pmic.h
>>> index 0f882c5649d..bd0583370ec 100644
>>> --- a/include/pbl/pmic.h
>>> +++ b/include/pbl/pmic.h
>>> @@ -9,6 +9,27 @@ struct pmic_config {
>>>  	u8 val;
>>>  };
>>>  
>>> +static void pmic_reg_read(struct pbl_i2c *i2c, int addr, u8 reg, void *buf)
>>
>> This should be static inline to avoid warnings for boards that don't use it.
>>
>>> +{
>>> +	int ret;
>>> +	struct i2c_msg msg[] = {
>>> +		{
>>> +			.addr = addr,
>>> +			.buf = &reg,
>>> +			.len = 1,
>>> +		}, {
>>> +			.addr = addr,
>>> +			.flags = I2C_M_RD,
>>> +			.buf = buf,
>>
>> U-Boot has buf of type u32 *. In your barebox implementation, only the
>> first byte is written, so this leaves 3 bytes potentially uninitialized.
>>
>> You likely want to change void *buf to u32 *val and then
>>
>>   u8 buf[1];
>>   /* i2c_xfer... */
>>   *val = buf[0];
> 
> When *buf is assumed to be a single byte only anyway then we can change
> its type to u8 *

For ease of porting from U-Boot board code, we should just keep the same prototype.

Cheers,
Ahmad

> 
> Sascha
> 
>>
>>> +			.len = 1,
>>> +		},
>>> +	};
>>> +
>>> +	ret = pbl_i2c_xfer(i2c, msg, ARRAY_SIZE(msg));
>>> +	if (ret != ARRAY_SIZE(msg))
>>> +		pr_err("Failed to read from pmic@%x: %d\n", addr, ret);
>>> +}
>>
>> Cheers,
>> Ahmad
>>
>>> +
>>>  static void pmic_reg_write(struct pbl_i2c *i2c, int addr, u8 reg, u8 val)
>>>  {
>>>  	int ret;
>>
>> -- 
>> Pengutronix e.K.                           |                             |
>> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
>> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
>> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
>>
>>
>>
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-02  9:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-19 16:11 [PATCH] i2c: add pmic_reg_read() for PBL use Bastian Krause
2023-12-19 16:21 ` Ahmad Fatoum
2024-01-02  9:11   ` Sascha Hauer
2024-01-02  9:13     ` Ahmad Fatoum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox