diff mbox

[U-Boot,2/6] power: Explicitly select pmic device's bus

Message ID 524D061F.7080204@denx.de
State Not Applicable
Headers show

Commit Message

Heiko Schocher Oct. 3, 2013, 5:52 a.m. UTC
Hello Lukasz,

Am 02.10.2013 17:11, schrieb Lukasz Majewski:
> Hi Leela,
>
>> The current pmic i2c code assumes the current i2c bus is
>> the same as the pmic device's bus. There is nothing ensuring
>> that to be true. Therefore, select the proper bus before performing
>> a transaction.
>>
>> Signed-off-by: Aaron Durbin<adurbin@chromium.org>
>> Signed-off-by: Simon Glass<sjg@chromium.org>
>> Signed-off-by: Leela Krishna Amudala<l.krishna@samsung.com>
>> Reviewed-by: Doug Anderson<dianders@google.com>
>> ---
>>   drivers/power/power_i2c.c |   62
>> +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57
>> insertions(+), 5 deletions(-)
>>
>> diff --git a/drivers/power/power_i2c.c b/drivers/power/power_i2c.c
>> index 47c606f..c22e01f 100644
>> --- a/drivers/power/power_i2c.c
>> +++ b/drivers/power/power_i2c.c
>> @@ -16,9 +16,45 @@
>>   #include<i2c.h>
>>   #include<compiler.h>
>>
>> +static int pmic_select(struct pmic *p)
>> +{
>> +	int ret, old_bus;
>> +
>> +	old_bus = i2c_get_bus_num();
>> +	if (old_bus != p->bus) {
>> +		debug("%s: Select bus %d\n", __func__, p->bus);
>> +		ret = i2c_set_bus_num(p->bus);
>> +		if (ret) {
>> +			debug("%s: Cannot select pmic %s, err %d\n",
>> +			      __func__, p->name, ret);
>> +			return -1;
>> +		}
>> +	}
>> +
>> +	return old_bus;
>> +}
>> +
>> +static int pmic_deselect(int old_bus)
>> +{
>> +	int ret;
>> +
>> +	if (old_bus != i2c_get_bus_num()) {
>> +		ret = i2c_set_bus_num(old_bus);
>> +		debug("%s: Select bus %d\n", __func__, old_bus);
>> +		if (ret) {
>> +			debug("%s: Cannot restore i2c bus, err %d\n",
>> +			      __func__, ret);
>> +			return -1;
>> +		}
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>>   int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
>>   {
>>   	unsigned char buf[4] = { 0 };
>> +	int ret, old_bus;
>>
>>   	if (check_reg(p, reg))
>>   		return -1;
>> @@ -52,23 +88,33 @@ int pmic_reg_write(struct pmic *p, u32 reg, u32
>> val) return -1;
>>   	}
>>
>> -	if (i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num))
>> +	old_bus = pmic_select(p);
>> +	if (old_bus<  0)
>>   		return -1;
>>
>> -	return 0;
>> +	ret = i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
>
> I'm wondering if setting the bus before each, single i2c write (when we
> usually perform several writes to one device) will not be an overkill
> (we search the ll_entry_count linker list each time to find max i2c
> adapter names) ?

Yes, maybe we could optimze this in drivers/i2c/i2c_core.c. It should be
enough to detect the max adapter once ... but it is not a "search"...
ll_entry_count() calculates the number ...

Looking in i2c_set_bus_num(), I think it can be optimized ...
lets speaking code:


So, first check, if we are on the correct bus, and return immediately!
What do you think?

Beside of that, pmic_select() does the check, if we are on the correct
bus too, and calls i2c_set_bus_num() only, if not ... so this is here
no problem ... but exactly I want to get rid of this code as it is in
pmic_select() someday, when all i2c drivers converted to the new i2c
framework. i2c_set_bus_num() should go static then in drivers/i2c/i2c_core.c
and i2c_read/write/... become a new "int bus" parameter ... but this
will be a big api change ... but will prevent exactly such code
all over the u-boot code ...

bye,
Heiko

Comments

Łukasz Majewski Oct. 3, 2013, 4:15 p.m. UTC | #1
Hi Heiko,

Sorry for a late reply.

> Hello Lukasz,
> 
> Am 02.10.2013 17:11, schrieb Lukasz Majewski:
> > Hi Leela,
> >
> >> The current pmic i2c code assumes the current i2c bus is
> >> the same as the pmic device's bus. There is nothing ensuring
> >> that to be true. Therefore, select the proper bus before performing
> >> a transaction.
> >>
> >> Signed-off-by: Aaron Durbin<adurbin@chromium.org>
> >> Signed-off-by: Simon Glass<sjg@chromium.org>
> >> Signed-off-by: Leela Krishna Amudala<l.krishna@samsung.com>
> >> Reviewed-by: Doug Anderson<dianders@google.com>
> >> ---
> >>   drivers/power/power_i2c.c |   62
> >> +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57
> >> insertions(+), 5 deletions(-)
> >>
> >> diff --git a/drivers/power/power_i2c.c b/drivers/power/power_i2c.c
> >> index 47c606f..c22e01f 100644
> >> --- a/drivers/power/power_i2c.c
> >> +++ b/drivers/power/power_i2c.c
> >> @@ -16,9 +16,45 @@
> >>   #include<i2c.h>
> >>   #include<compiler.h>
> >>
> >> +static int pmic_select(struct pmic *p)
> >> +{
> >> +	int ret, old_bus;
> >> +
> >> +	old_bus = i2c_get_bus_num();
> >> +	if (old_bus != p->bus) {
> >> +		debug("%s: Select bus %d\n", __func__, p->bus);
> >> +		ret = i2c_set_bus_num(p->bus);
> >> +		if (ret) {
> >> +			debug("%s: Cannot select pmic %s, err
> >> %d\n",
> >> +			      __func__, p->name, ret);
> >> +			return -1;
> >> +		}
> >> +	}
> >> +
> >> +	return old_bus;
> >> +}
> >> +
> >> +static int pmic_deselect(int old_bus)
> >> +{
> >> +	int ret;
> >> +
> >> +	if (old_bus != i2c_get_bus_num()) {
> >> +		ret = i2c_set_bus_num(old_bus);
> >> +		debug("%s: Select bus %d\n", __func__, old_bus);
> >> +		if (ret) {
> >> +			debug("%s: Cannot restore i2c bus, err
> >> %d\n",
> >> +			      __func__, ret);
> >> +			return -1;
> >> +		}
> >> +	}
> >> +
> >> +	return 0;
> >> +}
> >> +
> >>   int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
> >>   {
> >>   	unsigned char buf[4] = { 0 };
> >> +	int ret, old_bus;
> >>
> >>   	if (check_reg(p, reg))
> >>   		return -1;
> >> @@ -52,23 +88,33 @@ int pmic_reg_write(struct pmic *p, u32 reg, u32
> >> val) return -1;
> >>   	}
> >>
> >> -	if (i2c_write(pmic_i2c_addr, reg, 1, buf,
> >> pmic_i2c_tx_num))
> >> +	old_bus = pmic_select(p);
> >> +	if (old_bus<  0)
> >>   		return -1;
> >>
> >> -	return 0;
> >> +	ret = i2c_write(pmic_i2c_addr, reg, 1, buf,
> >> pmic_i2c_tx_num);
> >
> > I'm wondering if setting the bus before each, single i2c write
> > (when we usually perform several writes to one device) will not be
> > an overkill (we search the ll_entry_count linker list each time to
> > find max i2c adapter names) ?
> 
> Yes, maybe we could optimze this in drivers/i2c/i2c_core.c. It should
> be enough to detect the max adapter once ... but it is not a
> "search"... ll_entry_count() calculates the number ...

Yes, you are right. I've overlooked it.

With -Os compiler flag this compiles to a few ASM instructions.
Obviously it is NOT a performance killer :-) (I made unnecessary
fuzzz... sorry).

> 
> Looking in i2c_set_bus_num(), I think it can be optimized ...
> lets speaking code:
> 
> diff --git a/drivers/i2c/i2c_core.c b/drivers/i2c/i2c_core.c
> index d1072e8..170423a 100644
> --- a/drivers/i2c/i2c_core.c
> +++ b/drivers/i2c/i2c_core.c
> @@ -278,20 +278,22 @@ unsigned int i2c_get_bus_num(void)
>    */
>   int i2c_set_bus_num(unsigned int bus)
>   {
> -       int max = ll_entry_count(struct i2c_adapter, i2c);
> +       int max;
> +
> +       if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
> +               return 0;

This looks nice.

> 
> -       if (I2C_ADAPTER(bus) >= max) {
> -               printf("Error, wrong i2c adapter %d max %d
> possible\n",
> -                      I2C_ADAPTER(bus), max);
> -               return -2;
> -       }
>   #ifndef CONFIG_SYS_I2C_DIRECT_BUS
>          if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
>                  return -1;
>   #endif
> 
> -       if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
> -               return 0;
> +       max = ll_entry_count(struct i2c_adapter, i2c);
> +       if (I2C_ADAPTER(bus) >= max) {
> +               printf("Error, wrong i2c adapter %d max %d
> possible\n",
> +                      I2C_ADAPTER(bus), max);
> +               return -2;
> +       }
> 
>   #ifndef CONFIG_SYS_I2C_DIRECT_BUS
>          i2c_mux_disconnet_all();
> 
> So, first check, if we are on the correct bus, and return immediately!
> What do you think?

I think that it is acceptable.

> 
> Beside of that, pmic_select() does the check, if we are on the correct
> bus too, and calls i2c_set_bus_num() only, if not ... so this is here
> no problem ... 

Yes, I see.

> but exactly I want to get rid of this code as it is in
> pmic_select() someday, when all i2c drivers converted to the new i2c
> framework.

My 2 cents. I understand that pmic_select() preserves old i2c bus
number, when PMIC performs transmission. This is probably done to not
break the legacy code (where one driver assumed, that it is alone).

If this is necessary, then I'm OK with this. However I personally think,
that drivers shall call API functions from i2c core (like i2c_bus_num())
only with bus number to switch and do not store and preserve the i2c
value. This is my personal comment.


> i2c_set_bus_num() should go static then in
> drivers/i2c/i2c_core.c and i2c_read/write/... become a new "int bus"
> parameter ... but this will be a big api change ... but will prevent
> exactly such code all over the u-boot code ...
> 
> bye,
> Heiko
Heiko Schocher Oct. 4, 2013, 5:23 a.m. UTC | #2
Hello Lukasz,

Am 03.10.2013 18:15, schrieb Lukasz Majewski:
> Hi Heiko,
>
> Sorry for a late reply.
>
>> Hello Lukasz,
>>
>> Am 02.10.2013 17:11, schrieb Lukasz Majewski:
>>> Hi Leela,
>>>
>>>> The current pmic i2c code assumes the current i2c bus is
>>>> the same as the pmic device's bus. There is nothing ensuring
>>>> that to be true. Therefore, select the proper bus before performing
>>>> a transaction.
>>>>
>>>> Signed-off-by: Aaron Durbin<adurbin@chromium.org>
>>>> Signed-off-by: Simon Glass<sjg@chromium.org>
>>>> Signed-off-by: Leela Krishna Amudala<l.krishna@samsung.com>
>>>> Reviewed-by: Doug Anderson<dianders@google.com>
>>>> ---
>>>>    drivers/power/power_i2c.c |   62
>>>> +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57
>>>> insertions(+), 5 deletions(-)
>>>>
>>>> diff --git a/drivers/power/power_i2c.c b/drivers/power/power_i2c.c
>>>> index 47c606f..c22e01f 100644
>>>> --- a/drivers/power/power_i2c.c
>>>> +++ b/drivers/power/power_i2c.c
[...]
>> Yes, maybe we could optimze this in drivers/i2c/i2c_core.c. It should
>> be enough to detect the max adapter once ... but it is not a
>> "search"... ll_entry_count() calculates the number ...
>
> Yes, you are right. I've overlooked it.
>
> With -Os compiler flag this compiles to a few ASM instructions.
> Obviously it is NOT a performance killer :-) (I made unnecessary
> fuzzz... sorry).

No problem!

>> Looking in i2c_set_bus_num(), I think it can be optimized ...
>> lets speaking code:
>>
>> diff --git a/drivers/i2c/i2c_core.c b/drivers/i2c/i2c_core.c
>> index d1072e8..170423a 100644
>> --- a/drivers/i2c/i2c_core.c
>> +++ b/drivers/i2c/i2c_core.c
>> @@ -278,20 +278,22 @@ unsigned int i2c_get_bus_num(void)
>>     */
>>    int i2c_set_bus_num(unsigned int bus)
>>    {
>> -       int max = ll_entry_count(struct i2c_adapter, i2c);
>> +       int max;
>> +
>> +       if ((bus == I2C_BUS)&&  (I2C_ADAP->init_done>  0))
>> +               return 0;
>
> This looks nice.

Ok! I post soon a patch for it ...

>> -       if (I2C_ADAPTER(bus)>= max) {
>> -               printf("Error, wrong i2c adapter %d max %d
>> possible\n",
>> -                      I2C_ADAPTER(bus), max);
>> -               return -2;
>> -       }
>>    #ifndef CONFIG_SYS_I2C_DIRECT_BUS
>>           if (bus>= CONFIG_SYS_NUM_I2C_BUSES)
>>                   return -1;
>>    #endif
>>
>> -       if ((bus == I2C_BUS)&&  (I2C_ADAP->init_done>  0))
>> -               return 0;
>> +       max = ll_entry_count(struct i2c_adapter, i2c);
>> +       if (I2C_ADAPTER(bus)>= max) {
>> +               printf("Error, wrong i2c adapter %d max %d
>> possible\n",
>> +                      I2C_ADAPTER(bus), max);
>> +               return -2;
>> +       }
>>
>>    #ifndef CONFIG_SYS_I2C_DIRECT_BUS
>>           i2c_mux_disconnet_all();
>>
>> So, first check, if we are on the correct bus, and return immediately!
>> What do you think?
>
> I think that it is acceptable.

Good.

>> Beside of that, pmic_select() does the check, if we are on the correct
>> bus too, and calls i2c_set_bus_num() only, if not ... so this is here
>> no problem ...
>
> Yes, I see.
>
>> but exactly I want to get rid of this code as it is in
>> pmic_select() someday, when all i2c drivers converted to the new i2c
>> framework.
>
> My 2 cents. I understand that pmic_select() preserves old i2c bus
> number, when PMIC performs transmission. This is probably done to not
> break the legacy code (where one driver assumed, that it is alone).
>
> If this is necessary, then I'm OK with this. However I personally think,
> that drivers shall call API functions from i2c core (like i2c_bus_num())
> only with bus number to switch and do not store and preserve the i2c
> value. This is my personal comment.

Full Ack. I am just thinking, that we can get rid of such constructs,
independent of the new i2c framework switch. We just need to introduce
a "current_i2c_cmd_bus" in common/cmd_i2c.c. This var stores the
current i2c bus where i2c commands are executed ... and all other
subsystems, which use the i2c_api can call i2c_set_bus_num() without
a previous "save old bus" and after the i2c bus usage a "restore i2c
bus" ... I try to look into this, maybe we can do this before all
i2c drivers are ported to the new framework ...

bye,
Heiko
Łukasz Majewski Oct. 4, 2013, 8:58 a.m. UTC | #3
Hi Heiko,

> Hello Lukasz,
> 
> Am 03.10.2013 18:15, schrieb Lukasz Majewski:
> > Hi Heiko,
> >
> > Sorry for a late reply.
> >
> >> Hello Lukasz,
> >>
> >> Am 02.10.2013 17:11, schrieb Lukasz Majewski:
> >>> Hi Leela,
> >>>
> >>>> The current pmic i2c code assumes the current i2c bus is
> >>>> the same as the pmic device's bus. There is nothing ensuring
> >>>> that to be true. Therefore, select the proper bus before
> >>>> performing a transaction.
> >>>>
> >>>> Signed-off-by: Aaron Durbin<adurbin@chromium.org>
> >>>> Signed-off-by: Simon Glass<sjg@chromium.org>
> >>>> Signed-off-by: Leela Krishna Amudala<l.krishna@samsung.com>
> >>>> Reviewed-by: Doug Anderson<dianders@google.com>
> >>>> ---
> >>>>    drivers/power/power_i2c.c |   62
> >>>> +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 57
> >>>> insertions(+), 5 deletions(-)
> >>>>
> >>>> diff --git a/drivers/power/power_i2c.c
> >>>> b/drivers/power/power_i2c.c index 47c606f..c22e01f 100644
> >>>> --- a/drivers/power/power_i2c.c
> >>>> +++ b/drivers/power/power_i2c.c
> [...]
> >> Yes, maybe we could optimze this in drivers/i2c/i2c_core.c. It
> >> should be enough to detect the max adapter once ... but it is not a
> >> "search"... ll_entry_count() calculates the number ...
> >
> > Yes, you are right. I've overlooked it.
> >
> > With -Os compiler flag this compiles to a few ASM instructions.
> > Obviously it is NOT a performance killer :-) (I made unnecessary
> > fuzzz... sorry).
> 
> No problem!
> 
> >> Looking in i2c_set_bus_num(), I think it can be optimized ...
> >> lets speaking code:
> >>
> >> diff --git a/drivers/i2c/i2c_core.c b/drivers/i2c/i2c_core.c
> >> index d1072e8..170423a 100644
> >> --- a/drivers/i2c/i2c_core.c
> >> +++ b/drivers/i2c/i2c_core.c
> >> @@ -278,20 +278,22 @@ unsigned int i2c_get_bus_num(void)
> >>     */
> >>    int i2c_set_bus_num(unsigned int bus)
> >>    {
> >> -       int max = ll_entry_count(struct i2c_adapter, i2c);
> >> +       int max;
> >> +
> >> +       if ((bus == I2C_BUS)&&  (I2C_ADAP->init_done>  0))
> >> +               return 0;
> >
> > This looks nice.
> 
> Ok! I post soon a patch for it ...
> 
> >> -       if (I2C_ADAPTER(bus)>= max) {
> >> -               printf("Error, wrong i2c adapter %d max %d
> >> possible\n",
> >> -                      I2C_ADAPTER(bus), max);
> >> -               return -2;
> >> -       }
> >>    #ifndef CONFIG_SYS_I2C_DIRECT_BUS
> >>           if (bus>= CONFIG_SYS_NUM_I2C_BUSES)
> >>                   return -1;
> >>    #endif
> >>
> >> -       if ((bus == I2C_BUS)&&  (I2C_ADAP->init_done>  0))
> >> -               return 0;
> >> +       max = ll_entry_count(struct i2c_adapter, i2c);
> >> +       if (I2C_ADAPTER(bus)>= max) {
> >> +               printf("Error, wrong i2c adapter %d max %d
> >> possible\n",
> >> +                      I2C_ADAPTER(bus), max);
> >> +               return -2;
> >> +       }
> >>
> >>    #ifndef CONFIG_SYS_I2C_DIRECT_BUS
> >>           i2c_mux_disconnet_all();
> >>
> >> So, first check, if we are on the correct bus, and return
> >> immediately! What do you think?
> >
> > I think that it is acceptable.
> 
> Good.
> 
> >> Beside of that, pmic_select() does the check, if we are on the
> >> correct bus too, and calls i2c_set_bus_num() only, if not ... so
> >> this is here no problem ...
> >
> > Yes, I see.
> >
> >> but exactly I want to get rid of this code as it is in
> >> pmic_select() someday, when all i2c drivers converted to the new
> >> i2c framework.
> >
> > My 2 cents. I understand that pmic_select() preserves old i2c bus
> > number, when PMIC performs transmission. This is probably done to
> > not break the legacy code (where one driver assumed, that it is
> > alone).
> >
> > If this is necessary, then I'm OK with this. However I personally
> > think, that drivers shall call API functions from i2c core (like
> > i2c_bus_num()) only with bus number to switch and do not store and
> > preserve the i2c value. This is my personal comment.
> 
> Full Ack. I am just thinking, that we can get rid of such constructs,
> independent of the new i2c framework switch. We just need to introduce
> a "current_i2c_cmd_bus" in common/cmd_i2c.c. This var stores the
> current i2c bus where i2c commands are executed ... 

I think that "last used bus" variable shall be stored/managed at
i2c_core.c. I can use i2c without cmd_i2c.c compiled (as it is with
pmic and fuel gauge, which use different buses).

> and all other
> subsystems, which use the i2c_api can call i2c_set_bus_num() without
> a previous "save old bus" and after the i2c bus usage a "restore i2c
> bus" ...


> I try to look into this, maybe we can do this before all
> i2c drivers are ported to the new framework ...

Ok, lets wait for a patch.

> 
> bye,
> Heiko
Heiko Schocher Oct. 4, 2013, 9:35 a.m. UTC | #4
Hello Lukasz,

Am 04.10.2013 10:58, schrieb Lukasz Majewski:
> Hi Heiko,
>
>> Hello Lukasz,
>>
>> Am 03.10.2013 18:15, schrieb Lukasz Majewski:
>>> Hi Heiko,
>>>
>>> Sorry for a late reply.
>>>
>>>> Hello Lukasz,
>>>>
>>>> Am 02.10.2013 17:11, schrieb Lukasz Majewski:
>>>>> Hi Leela,
[...]
>>>> but exactly I want to get rid of this code as it is in
>>>> pmic_select() someday, when all i2c drivers converted to the new
>>>> i2c framework.
>>>
>>> My 2 cents. I understand that pmic_select() preserves old i2c bus
>>> number, when PMIC performs transmission. This is probably done to
>>> not break the legacy code (where one driver assumed, that it is
>>> alone).
>>>
>>> If this is necessary, then I'm OK with this. However I personally
>>> think, that drivers shall call API functions from i2c core (like
>>> i2c_bus_num()) only with bus number to switch and do not store and
>>> preserve the i2c value. This is my personal comment.
>>
>> Full Ack. I am just thinking, that we can get rid of such constructs,
>> independent of the new i2c framework switch. We just need to introduce
>> a "current_i2c_cmd_bus" in common/cmd_i2c.c. This var stores the
>> current i2c bus where i2c commands are executed ...
>
> I think that "last used bus" variable shall be stored/managed at
> i2c_core.c. I can use i2c without cmd_i2c.c compiled (as it is with

Thats the case for the new framework! It stores the current bus, and
so i2c_set_bus_num() can decide, if it is necessary to switch to
a new bus ...

Additionally to get rid of such "store oldbus, switch to new, restore
old bus" constructs, the i2c commands needs to store somewhere, which
i2c bus the i2c commands currently use, because you can do a "i2c md ..."
then a "date" (maybe on antoher i2c bus) and then again a "i2c md ..."
I think, thats the real historical reason for doing all over the code
such "save oldbus" code ...

That var (saying i2c_cur_cmd_bus) can easily stored in common/cmd_i2c.c,
as commands are only useable after relocation. So, we must just add a
i2c_set_bus_num(i2c_cur_cmd_bus) call before a command is executed ...
That can be done in common/cmd_i2c.c also ...

So, in the end, before every i2c access we call i2c_set_bus_num()
and have no longer to store an "old bus" ... and if we have reached
this point, we can make i2c_set_bus_num() static, and add to the
i2c api a new first "int bus" parameter .... and can delete all
i2c_set_bus_num() calls ... so every i2c device must know, on which
i2c bus it works ... and as a bonus, we can get rid of all i2c_init()
calls all over around the code, as i2c_set_bus_num() checks, if the
drivers is initialized or not, if not initialize it, look if i2c
muxes are involved on this bus, and so on ...

> pmic and fuel gauge, which use different buses).

Yep, and its enough to call i2c_set_bus_num() before you want to access
a device ... i2c_set_bus_num() do all necessary steps for you.

>> and all other
>> subsystems, which use the i2c_api can call i2c_set_bus_num() without
>> a previous "save old bus" and after the i2c bus usage a "restore i2c
>> bus" ...
>
>
>> I try to look into this, maybe we can do this before all
>> i2c drivers are ported to the new framework ...
>
> Ok, lets wait for a patch.

I have such a patch in my queue, but it works only for drivers, which use
the new framework I think ... so I must think about it, if this works
for old style drivers too ...

bye,
Heiko
diff mbox

Patch

diff --git a/drivers/i2c/i2c_core.c b/drivers/i2c/i2c_core.c
index d1072e8..170423a 100644
--- a/drivers/i2c/i2c_core.c
+++ b/drivers/i2c/i2c_core.c
@@ -278,20 +278,22 @@  unsigned int i2c_get_bus_num(void)
   */
  int i2c_set_bus_num(unsigned int bus)
  {
-       int max = ll_entry_count(struct i2c_adapter, i2c);
+       int max;
+
+       if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
+               return 0;

-       if (I2C_ADAPTER(bus) >= max) {
-               printf("Error, wrong i2c adapter %d max %d possible\n",
-                      I2C_ADAPTER(bus), max);
-               return -2;
-       }
  #ifndef CONFIG_SYS_I2C_DIRECT_BUS
         if (bus >= CONFIG_SYS_NUM_I2C_BUSES)
                 return -1;
  #endif

-       if ((bus == I2C_BUS) && (I2C_ADAP->init_done > 0))
-               return 0;
+       max = ll_entry_count(struct i2c_adapter, i2c);
+       if (I2C_ADAPTER(bus) >= max) {
+               printf("Error, wrong i2c adapter %d max %d possible\n",
+                      I2C_ADAPTER(bus), max);
+               return -2;
+       }

  #ifndef CONFIG_SYS_I2C_DIRECT_BUS
         i2c_mux_disconnet_all();