usb: ehci-mx6: replace regulator_set_enable with *_if_allowed
Subsystems (2)
PATCH 1/1 usb: ehci-mx6: replace regulator_set_enable with *_if_allowed
regulator_set_enable_if_allowed already handles cases when the regulator is already enabled, or already disabled, and does not treat these as errors.
With this change, the driver can work correctly even if the regulator is already taken or already disabled by another consumer.
---
Hi Tim,
I have not tested this as I do not have a mx6 board. can you please try it ?
Thanks,
Eugen
drivers/usb/host/ehci-mx6.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/usb/host/ehci-mx6.c +8 -8
@@ -480,9 +480,9 @@ static int mx6_init_after_reset(struct ehci_ctrl *dev)#if CONFIG_IS_ENABLED (DM_REGULATOR )if (priv->vbus_supply) {int ret; - ret =regulator_set_enable (priv->vbus_supply, - (type ==USB_INIT_DEVICE ) ? - false : true); + ret =regulator_set_enable_if_allowed (priv->vbus_supply, + (type ==USB_INIT_DEVICE ) ? + false : true);if (ret && ret != -ENOSYS ) {printf ("Error enabling VBUS supply (ret=%i)\n" , ret);return ret;
@@ -707,9 +707,9 @@ static int ehci_usb_probe(struct udevice *dev)#if CONFIG_IS_ENABLED (DM_REGULATOR )if (priv->vbus_supply) { - ret =regulator_set_enable (priv->vbus_supply, - (type ==USB_INIT_DEVICE ) ? - false : true); + ret =regulator_set_enable_if_allowed (priv->vbus_supply, + (type ==USB_INIT_DEVICE ) ? + false : true);if (ret && ret != -ENOSYS ) {printf ("Error enabling VBUS supply (ret=%i)\n" , ret); goto err_clk;
@@ -748,7 +748,7 @@ err_regulator:#endif #if CONFIG_IS_ENABLED (DM_REGULATOR )if (priv->vbus_supply) -regulator_set_enable (priv->vbus_supply, false); +regulator_set_enable_if_allowed (priv->vbus_supply, false);#endif err_clk: #if CONFIG_IS_ENABLED(CLK)
@@ -772,7 +772,7 @@ int ehci_usb_remove(struct udevice *dev)#if CONFIG_IS_ENABLED (DM_REGULATOR )if (priv->vbus_supply) -regulator_set_enable (priv->vbus_supply, false); +regulator_set_enable_if_allowed (priv->vbus_supply, false);#endif #if CONFIG_IS_ENABLED (CLK )
Signature · 1 line
2.34.1
Quoted 5 lines · Eugen Hristev
regulator_set_enable_if_allowedalready handles cases when the regulator is already enabled, or already disabled, and does not treat these as errors.
With this change, the driver can work correctly even if the regulator is already taken or already disabled by another consumer.
Can that ever happen for Vbus supply (the 5V coming out of USB port) ? Can you elaborate how ?
Quoted 9 lines · Marek Vasut
Quoted 5 lines · Eugen Hristev
regulator_set_enable_if_allowedalready handles cases when the regulator is already enabled, or already disabled, and does not treat these as errors.
With this change, the driver can work correctly even if the regulator is already taken or already disabled by another consumer.Can that ever happen for Vbus supply (the 5V coming out of USB port) ? Can you elaborate how ?
Hi Marek,
Recently I developed a series of patches to add a reference counter for regulators :
https://marc.info/?l=u-boot&m=168191189309879&w=2
But with this series, having a regulator already enabled or already disabled results in an error returned by regulator_set_enable
Hence, one option is to replace calls with regulator_set_enable_if_allowed
There is a discussion ongoing here:
https://marc.info/?l=u-boot&m=168295920316621&w=2
Eugen
Quoted 8 lines · Eugen Hristev
Quoted 7 lines · Marek Vasut
Quoted 5 lines · Eugen Hristev
regulator_set_enable_if_allowedalready handles cases when the regulator is already enabled, or already disabled, and does not treat these as errors.
With this change, the driver can work correctly even if the regulator is already taken or already disabled by another consumer.
Quoted 5 lines
Quoted 2 lines
Can that ever happen for Vbus supply (the 5V coming out of USB port) ? Can you elaborate how ?
Hi Marek,
Hi,
Quoted 3 lines
Recently I developed a series of patches to add a reference counter for regulators :
Ah yes, this is super cool stuff, thanks !
Quoted 7 lines
But with this series, having a regulator already enabled or already disabled results in an error returned by
regulator_set_enableHence, one option is to replace calls with
regulator_set_enable_if_allowedThere is a discussion ongoing here:
Pardon my ignorance, but uh ... how does Linux deal with regulators which are enabled by prior stage, like U-Boot ? Does it set their enable refcount to =1 or =0 ?
Quoted 25 lines · Marek Vasut
Quoted 8 lines · Eugen Hristev
Quoted 7 lines · Marek Vasut
Quoted 5 lines · Eugen Hristev
regulator_set_enable_if_allowedalready handles cases when the regulator is already enabled, or already disabled, and does not treat these as errors.
With this change, the driver can work correctly even if the regulator is already taken or already disabled by another consumer.Quoted 5 lines
Quoted 2 lines
Can that ever happen for Vbus supply (the 5V coming out of USB port) ? Can you elaborate how ?
Hi Marek,
Hi,
Quoted 3 lines
Recently I developed a series of patches to add a reference counter for regulators :
Ah yes, this is super cool stuff, thanks !
Quoted 2 lines
But with this series, having a regulator already enabled or already disabled results in an error returned by
regulator_set_enable
Quoted 7 lines
Quoted 5 lines
Hence, one option is to replace calls with
regulator_set_enable_if_allowedThere is a discussion ongoing here:
Pardon my ignorance, but uh ... how does Linux deal with regulators which are enabled by prior stage, like U-Boot ? Does it set their enable refcount to =1 or =0 ?
Please correct me if I am wrong, I did not dig too much into regulators, but , from my understanding :
Normally software(in this case Linux) reads the state of the regulator when it's probed, and sets the refcount accordingly. If the state cannot be read then regulator-boot-on tells Linux that it should have been left enabled by prior stage In Uboot I believe that regulators that have 'regulator-boot-on' are added to a list and set to enable during probe, and the probe is forced even if Uboot normally uses lazy probing (should have the same behavior for regulator-always-on property)
So if a regulator is enabled by an initial stage but it's not described with 'regulator-boot-on' , appears to be a bad hardware description in DT.
Quoted 32 lines · Eugen Hristev
Quoted 31 lines · Marek Vasut
Quoted 8 lines · Eugen Hristev
Quoted 7 lines · Marek Vasut
Quoted 5 lines · Eugen Hristev
regulator_set_enable_if_allowedalready handles cases when the regulator is already enabled, or already disabled, and does not treat these as errors.
With this change, the driver can work correctly even if the regulator is already taken or already disabled by another consumer.Quoted 5 lines
Quoted 2 lines
Can that ever happen for Vbus supply (the 5V coming out of USB port) ? Can you elaborate how ?
Hi Marek,
Hi,
Quoted 3 lines
Recently I developed a series of patches to add a reference counter for regulators :
Ah yes, this is super cool stuff, thanks !
Quoted 7 lines
But with this series, having a regulator already enabled or already disabled results in an error returned by
regulator_set_enableHence, one option is to replace calls with
regulator_set_enable_if_allowedThere is a discussion ongoing here:
Quoted 8 lines
Quoted 1 line
Pardon my ignorance, but uh ... how does Linux deal with regulators which are enabled by prior stage, like U-Boot ? Does it set their enable refcount to =1 or =0 ?
Please correct me if I am wrong, I did not dig too much into regulators, but , from my understanding :
Normally software(in this case Linux) reads the state of the regulator when it's probed, and sets the refcount accordingly. If the state cannot be read then regulator-boot-on tells Linux that it should have been left enabled by prior stage In Uboot I believe that regulators that have 'regulator-boot-on' are added to a list and set to enable during probe, and the probe is forced even if Uboot normally uses lazy probing (should have the same behavior for regulator-always-on property)
So if a regulator is enabled by an initial stage but it's not described with 'regulator-boot-on' , appears to be a bad hardware description in DT.
Thanks for checking this. It seems like Tim found the root cause of the problem in the meantime, so let's see where that discussion moves on to.
Quoted 8 lines · Eugen Hristev
regulator_set_enable_if_allowedalready handles cases when the regulator is already enabled, or already disabled, and does not treat these as errors.
With this change, the driver can work correctly even if the regulator is already taken or already disabled by another consumer.Signed-off-by Eugen Hristev <[email protected]>
Quoted 8 lines
---
Hi Tim,I have not tested this as I do not have a mx6 board. can you please try it ?
Thanks,
Eugen
Eugen,
This does resolve the issue that occurs after your refcount series [1].
However after thinking about this more and seeing Marek's responses this wasn't an issue of multiple consumers sharing the same regulator. Instead this particular issue was that the vbus regulator is getting enabled twice without being disabled. Adding a print in regulator_set_enable shows me:
drivers/power/regulator/regulator-uclass.c +1 -0
@@ -165,6 +165,7 @@ int regulator_set_enable(struct udevice *dev, bool enable)struct dm_regulator_uclass_plat *uc_pdata;int ret, old_enable =0 ; +printf ("%s %s %s\n" , __func__, dev->name, enable ?"enable" :"disable" );if (!ops || !ops->set_enable)return -ENOSYS ;
u-boot=> usb start
starting USB...
Bus usb@32e40000: regulator_set_enable regulator-usb-otg1 enable ^^^ from ehci_usb_probe
Bus usb@32e50000: regulator_set_enable regulator-usb-otg2 enable ^^^ from ehci_usb_proberegulator_set_enable regulator-usb-otg2 enable
^^^ from mx6_init_after_reset - 2nd enable without a disable
So while I think this patch does make sense to cover the case where a regulator could be shared there probably is a follow-on patch needed to balance the regulator calls (unrelated to your series).
Marek,
Should vbus regulator enable really be in init_after_reset call?
Best Regards,
Tim
[1] https://patchwork.ozlabs.org/project/uboot/list/?series=351536
Quoted 47 lines · Tim Harvey
Quoted 8 lines · Eugen Hristev
regulator_set_enable_if_allowedalready handles cases when the regulator is already enabled, or already disabled, and does not treat these as errors.
With this change, the driver can work correctly even if the regulator is already taken or already disabled by another consumer.Signed-off-by Eugen Hristev <[email protected]>Quoted 8 lines
---
Hi Tim,I have not tested this as I do not have a mx6 board. can you please try it ?
Thanks,
EugenEugen,
This does resolve the issue that occurs after your refcount series [1].
However after thinking about this more and seeing Marek's responses this wasn't an issue of multiple consumers sharing the same regulator. Instead this particular issue was that the vbus regulator is getting enabled twice without being disabled. Adding a print in
regulator_set_enableshows me:drivers/power/regulator/regulator-uclass.c +1
-0@@ -165,6 +165,7 @@ int regulator_set_enable(struct udevice *dev, bool enable)struct dm_regulator_uclass_plat *uc_pdata;int ret, old_enable =0 ; +printf ("%s %s %s\n" , __func__, dev->name, enable ?"enable" :"disable" );if (!ops || !ops->set_enable)return -ENOSYS ;u-boot=> usb start
starting USB...
Bus usb@32e40000:regulator_set_enableregulator-usb-otg1 enable ^^^ fromehci_usb_probe
Bus usb@32e50000:regulator_set_enableregulator-usb-otg2 enable ^^^ fromehci_usb_proberegulator_set_enableregulator-usb-otg2 enable
^^^ frommx6_init_after_reset- 2nd enable without a disable
Quoted 2 lines
So while I think this patch does make sense to cover the case where a regulator could be shared
Does such a case really still exist after the discovery you made above ?
Quoted 6 lines
there probably is a follow-on patch needed
to balance the regulator calls (unrelated to your series).Marek,
Should vbus regulator enable really be in
init_after_resetcall?
Yes, but I am not entirely convinced the VBUS should be enabled in ehci_usb_probe(), because in ehci_usb_probe() resp. in ehci_register() which is called at the end, we might detect that the port is configured as PERIPHERAL and we don't want to enable VBUS in that case .
So I suspect regulator_set_enable() should rather be dropped from ehci_usb_probe() . What do you think ?
Quoted 20 lines · Marek Vasut
Quoted 19 lines · Tim Harvey
Quoted 16 lines · Eugen Hristev
regulator_set_enable_if_allowedalready handles cases when the regulator is already enabled, or already disabled, and does not treat these as errors.
With this change, the driver can work correctly even if the regulator is already taken or already disabled by another consumer.Signed-off-by Eugen Hristev <[email protected]>---
Hi Tim,I have not tested this as I do not have a mx6 board. can you please try it ?
Thanks,
Eugen
Quoted 8 lines
Quoted 8 lines
Eugen,
This does resolve the issue that occurs after your refcount series [1].
However after thinking about this more and seeing Marek's responses this wasn't an issue of multiple consumers sharing the same regulator. Instead this particular issue was that the vbus regulator is getting enabled twice without being disabled.
This kind of issues can be uncovered when we add the refcounter :)
On one hand I am happy to uncover such bugs.
However, this leads me to wonder, even without a refcount, even without my series, enabling a regulator that is already enabled should return -EALREADY
On the other hand, it appears it will take longer to get the refcount applied though.
Adding a print in
Quoted 37 lines
Quoted 22 lines
regulator_set_enableshows me:drivers/power/regulator/regulator-uclass.c +1
-0@@ -165,6 +165,7 @@ int regulator_set_enable(struct udevice *dev, bool enable) struct dm_regulator_uclass_plat *uc_pdata; int ret, old_enable = 0; +printf ("%s %s %s\n" , __func__, dev->name, enable ?"enable" :"disable" ); if (!ops || !ops->set_enable)return
-ENOSYS;u-boot=> usb start
starting USB...
Bus usb@32e40000:regulator_set_enableregulator-usb-otg1 enable ^^^ fromehci_usb_probe
Bus usb@32e50000:regulator_set_enableregulator-usb-otg2 enable ^^^ fromehci_usb_proberegulator_set_enableregulator-usb-otg2 enable
^^^ frommx6_init_after_reset- 2nd enable without a disableQuoted 2 lines
So while I think this patch does make sense to cover the case where a regulator could be shared
Does such a case really still exist after the discovery you made above ?
Quoted 6 lines
there probably is a follow-on patch needed
to balance the regulator calls (unrelated to your series).Marek,
Should vbus regulator enable really be in
init_after_resetcall?Yes, but I am not entirely convinced the VBUS should be enabled in
ehci_usb_probe(), because inehci_usb_probe()resp. inehci_register()which is called at the end, we might detect that the port is configured as PERIPHERAL and we don't want to enable VBUS in that case .So I suspect
regulator_set_enable()should rather be dropped fromehci_usb_probe(). What do you think ?
Quoted 23 lines · Eugen Hristev
Quoted 22 lines · Marek Vasut
Quoted 21 lines · Tim Harvey
Quoted 8 lines · Eugen Hristev
regulator_set_enable_if_allowedalready handles cases when the regulator is already enabled, or already disabled, and does not treat these as errors.
With this change, the driver can work correctly even if the regulator is already taken or already disabled by another consumer.Signed-off-by Eugen Hristev <[email protected]>Quoted 8 lines
---
Hi Tim,I have not tested this as I do not have a mx6 board. can you please try it ?
Thanks,
EugenEugen,
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
This does resolve the issue that occurs after your refcount series [1].
However after thinking about this more and seeing Marek's responses this wasn't an issue of multiple consumers sharing the same regulator. Instead this particular issue was that the vbus regulator is getting enabled twice without being disabled.
This kind of issues can be uncovered when we add the refcounter :)
Yeah, and that's a good thing. The refcounter is much appreciated (am I repeating myself?)
Quoted 3 lines
On one hand I am happy to uncover such bugs.
However, this leads me to wonder, even without a refcount, even without my series, enabling a regulator that is already enabled should return-EALREADY
I don't that so, you can have regulators which supply multiple things and which are claimed by multiple driver instances, at which point they will each enable the regulator (refcount++) and that shouldn't end up returning -EALREADY . Right ?
One such example is buck regulator supplying e.g. SD and eMMC Vcc .
Quoted 1 line
On the other hand, it appears it will take longer to get the refcount applied though.
I think we need to sort out the fallout, but that's probably still fine.
Quoted 31 lines · Marek Vasut
Quoted 30 lines · Eugen Hristev
Quoted 28 lines · Marek Vasut
Quoted 27 lines · Tim Harvey
Quoted 8 lines · Eugen Hristev
regulator_set_enable_if_allowedalready handles cases when the regulator is already enabled, or already disabled, and does not treat these as errors.
With this change, the driver can work correctly even if the regulator is already taken or already disabled by another consumer.Signed-off-by Eugen Hristev <[email protected]>Quoted 8 lines
---
Hi Tim,I have not tested this as I do not have a mx6 board. can you please try it ?
Thanks,
EugenEugen,
This does resolve the issue that occurs after your refcount series [1].
However after thinking about this more and seeing Marek's responses this wasn't an issue of multiple consumers sharing the same regulator. Instead this particular issue was that the vbus regulator is getting enabled twice without being disabled.
Quoted 8 lines
Quoted 1 line
This kind of issues can be uncovered when we add the refcounter :)
Yeah, and that's a good thing. The refcounter is much appreciated (am I repeating myself?)
Quoted 2 lines
On one hand I am happy to uncover such bugs.
However, this leads me to wonder, even without a refcount, even without my series, enabling a regulator that is already enabled should return-EALREADYI don't that so, you can have regulators which supply multiple things and which are claimed by multiple driver instances, at which point they will each enable the regulator (refcount++) and that shouldn't end up returning
-EALREADY. Right ?
That was my thought. And I was returning success if regulator was already enabled. Then Simon asked me to return error codes on each path (review on the v2 of the patches):
https://marc.info/?l=u-boot&m=168012024923488&w=2
Quoted 6 lines
One such example is buck regulator supplying e.g. SD and eMMC Vcc .
Quoted 1 line
On the other hand, it appears it will take longer to get the refcount applied though.
I think we need to sort out the fallout, but that's probably still fine.
Quoted 39 lines · Eugen Hristev
Quoted 38 lines · Marek Vasut
Quoted 23 lines · Eugen Hristev
Quoted 22 lines · Marek Vasut
Quoted 21 lines · Tim Harvey
Quoted 8 lines · Eugen Hristev
regulator_set_enable_if_allowedalready handles cases when the regulator is already enabled, or already disabled, and does not treat these as errors.
With this change, the driver can work correctly even if the regulator is already taken or already disabled by another consumer.Signed-off-by Eugen Hristev <[email protected]>Quoted 8 lines
---
Hi Tim,I have not tested this as I do not have a mx6 board. can you please try it ?
Thanks,
EugenEugen,
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
This does resolve the issue that occurs after your refcount series [1].
However after thinking about this more and seeing Marek's responses this wasn't an issue of multiple consumers sharing the same regulator. Instead this particular issue was that the vbus regulator is getting enabled twice without being disabled.
This kind of issues can be uncovered when we add the refcounter :)
Yeah, and that's a good thing. The refcounter is much appreciated (am I repeating myself?)
Quoted 2 lines
On one hand I am happy to uncover such bugs.
However, this leads me to wonder, even without a refcount, even without my series, enabling a regulator that is already enabled should return-EALREADY
Quoted 6 lines
Quoted 1 line
I don't that so, you can have regulators which supply multiple things and which are claimed by multiple driver instances, at which point they will each enable the regulator (refcount++) and that shouldn't end up returning
-EALREADY. Right ?That was my thought. And I was returning success if regulator was already enabled. Then Simon asked me to return error codes on each path (review on the v2 of the patches):
I would argue the regulator API should behave the same way as Linux one, to avoid confusing people developing on both. Simon ?