smiapp: Implement power-on and power-off sequences without runtime PM
PATCH 1/1 smiapp: Implement power-on and power-off sequences without runtime PM
Power on the sensor when the module is loaded and power it off when it is removed.
---
Hi Arnd and others,
The patch is tested with CONFIG_PM set, as the system does I was testing on did not boot with CONFIG_PM disabled. I'm not really too worried about this though, the patch is very simple.
Kind regards,
Sakari
drivers/media/i2c/smiapp/smiapp-core.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/media/i2c/smiapp/smiapp-core.c +12 -9
@@ -2741,8 +2741,6 @@ static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {* I2C Driver */ -#ifdef CONFIG_PM -static int smiapp_suspend (struct device *dev) {struct i2c_client *client =to_i2c_client (dev);
@@ -2783,13 +2781,6 @@ static int smiapp_resume(struct device *dev)return rval; } -#else - -#define smiapp_suspend NULL -#define smiapp_resume NULL - -#endif /* CONFIG_PM */ -static struct smiapp_hwconfig *smiapp_get_hwconfig (struct device *dev) {struct smiapp_hwconfig *hwcfg;
@@ -2915,7 +2906,11 @@ static int smiapp_probe(struct i2c_client *client,pm_runtime_enable (&client->dev); +#ifdef CONFIG_PM rval =pm_runtime_get_sync (&client->dev); +#else + rval =smiapp_power_on (&client->dev); +#endif if (rval <0 ) { rval = -ENODEV ; goto out_power_off;
@@ -3113,7 +3108,11 @@ static int smiapp_probe(struct i2c_client *client,smiapp_cleanup (sensor); out_power_off: +#ifdef CONFIG_PM pm_runtime_put (&client->dev); +#else +smiapp_power_off (&client->dev); +#endif pm_runtime_disable (&client->dev);return rval;
@@ -3127,7 +3126,11 @@ static int smiapp_remove(struct i2c_client *client)v4l2_async_unregister_subdev (subdev); +#ifdef CONFIG_PM pm_runtime_suspend (&client->dev); +#else +smiapp_power_off (&client->dev); +#endif pm_runtime_disable (&client->dev);for (i =0 ; i < sensor->ssds_used; i++) {
Signature · 1 line
2.1.4
Quoted 18 lines · CET Sakari Ailus
Power on the sensor when the module is loaded and power it off when it is removed.
Signed-off-by Sakari Ailus <[email protected]>---
Hi Arnd and others,The patch is tested with
CONFIG_PMset, as the system does I was testing on did not boot withCONFIG_PMdisabled. I'm not really too worried about this though, the patch is very simple.static struct
smiapp_hwconfig*smiapp_get_hwconfig(struct device *dev) {drivers/media/i2c/smiapp/smiapp-core.c +0
-0struct smiapp_hwconfig *hwcfg;@@ -2915,7 +2906,11 @@ static int smiapp_probe(struct i2c_client *client,
drivers/media/i2c/smiapp/smiapp-core.c static int smiapp_probe(struct i2c_client *client, · line 2916
Quoted 10 lines
drivers/media/i2c/smiapp/smiapp-core.c +4
-0pm_runtime_enable (&client->dev); +#ifdef CONFIG_PM rval =pm_runtime_get_sync (&client->dev); +#else + rval =smiapp_power_on (&client->dev); +#endif if (rval <0 ) { rval = -ENODEV ; goto out_power_off;
I would suggest writing this as
if (IS_ENABLED(CONFIG_PM)) rval = pm_runtime_get_sync(&client->dev); else rval = smiapp_power_on(&client->dev);
though that is a purely cosmetic change.
I think you are missing one other warning: with CONFIG_PM=y and CONFIG_PM_SLEEP=n, the smiapp_suspend/smiapp_resume functions are now unused and need to be marked as __maybe_unused.
Arnd
Hi Arnd,
Quoted 31 lines · On Friday 18 Nov 2016 17:09:01 Arnd Bergmann wrote:
Quoted 26 lines · CET Sakari Ailus
Power on the sensor when the module is loaded and power it off when it is removed.
Signed-off-by Sakari Ailus <[email protected]>---
Hi Arnd and others,The patch is tested with
CONFIG_PMset, as the system does I was testing on did not boot withCONFIG_PMdisabled. I'm not really too worried about this though, the patch is very simple.static struct
smiapp_hwconfig*smiapp_get_hwconfig(struct device *dev) {drivers/media/i2c/smiapp/smiapp-core.c +4
-0struct smiapp_hwconfig *hwcfg;@@ -2915,7 +2906,11 @@ static int smiapp_probe(struct i2c_client *client,pm_runtime_enable (&client->dev); +#ifdef CONFIG_PM rval =pm_runtime_get_sync (&client->dev); +#else + rval =smiapp_power_on (&client->dev); +#endif if (rval <0 ) { rval = -ENODEV ;Quoted 1 line
goto
out_power_off;I would suggest writing this as
Quoted 6 lines
if (IS_ENABLED(CONFIG_PM)) rval = pm_runtime_get_sync(&client->dev); else rval = smiapp_power_on(&client->dev);though that is a purely cosmetic change.
Are all drivers really supposed to code this kind of construct ? Shouldn't this be handled in the PM core ? A very naive approach would be to call .runtime_resume() and .runtime_suspend() from the non-CONFIG_PM versions of pm_runtime_enable() and pm_runtime_disable() respectively. I assume that would break things, but can't we implement something similar to that that wouldn't require all drivers to open-code it ?
Quoted 3 lines
I think you are missing one other warning: with
CONFIG_PM=yandCONFIG_PM_SLEEP=n, the smiapp_suspend/smiapp_resume functions are now unused and need to be marked as__maybe_unused.
Signature · 3 lines
Regards, Laurent Pinchart
Quoted 21 lines · CET Laurent Pinchart
Quoted 21 lines
Quoted 12 lines
drivers/media/i2c/smiapp/smiapp-core.c +4
-0@@ -2915,7 +2906,11 @@ static int smiapp_probe(struct i2c_client *client,pm_runtime_enable (&client->dev); +#ifdef CONFIG_PM rval =pm_runtime_get_sync (&client->dev); +#else + rval =smiapp_power_on (&client->dev); +#endif if (rval <0 ) { rval = -ENODEV ;Quoted 1 line
goto out_power_off;I would suggest writing this as
if (IS_ENABLED(CONFIG_PM)) rval = pm_runtime_get_sync(&client->dev); else rval = smiapp_power_on(&client->dev);
Quoted 8 lines
Quoted 1 line
though that is a purely cosmetic change.
Are all drivers really supposed to code this kind of construct ? Shouldn't this be handled in the PM core ? A very naive approach would be to call .runtime_resume() and .runtime_suspend() from the non-CONFIG_PM versions of
pm_runtime_enable()andpm_runtime_disable()respectively. I assume that would break things, but can't we implement something similar to that that wouldn't require all drivers to open-code it ?
I know nothing about the details of how the suspend/resume code should do this, I was just commenting on the syntax above, preferring an IS_ENABLED() check over an #ifdef.
Arnd
Hello,
(CC'ing the linux-pm mailing list)
Quoted 31 lines · On Tuesday 22 Nov 2016 21:58:32 Arnd Bergmann wrote:
Quoted 21 lines · CET Laurent Pinchart
Quoted 21 lines
Quoted 12 lines
drivers/media/i2c/smiapp/smiapp-core.c +4
-0@@ -2915,7 +2906,11 @@ static int smiapp_probe(struct i2c_client *client,pm_runtime_enable (&client->dev); +#ifdef CONFIG_PM rval =pm_runtime_get_sync (&client->dev); +#else + rval =smiapp_power_on (&client->dev); +#endif if (rval <0 ) { rval = -ENODEV ;Quoted 1 line
goto out_power_off;I would suggest writing this as
if (IS_ENABLED(CONFIG_PM)) rval = pm_runtime_get_sync(&client->dev); else rval = smiapp_power_on(&client->dev);Quoted 8 lines
Quoted 1 line
though that is a purely cosmetic change.
Are all drivers really supposed to code this kind of construct ? Shouldn't this be handled in the PM core ? A very naive approach would be to call .runtime_resume() and .runtime_suspend() from the non-CONFIG_PM versions of
pm_runtime_enable()andpm_runtime_disable()respectively. I assume that would break things, but can't we implement something similar to that that wouldn't require all drivers to open-code it ?
Quoted 3 lines
I know nothing about the details of how the suspend/resume code should do this, I was just commenting on the syntax above, preferring an
IS_ENABLED()check over an #ifdef.
Dear linux-pm developers, what's the suggested way to ensure that a runtime- pm-enabled driver can run fine on a system with CONFIG_PM disabled ?
Signature · 3 lines
Regards, Laurent Pinchart
Quoted 2 lines · Laurent Pinchart
Dear linux-pm developers, what's the suggested way to ensure that a runtime- pm-enabled driver can run fine on a system with
CONFIG_PMdisabled ?
The exact point of your question isn't entirely clear. In the most literal sense, the best ways to ensure this are (1) audit the code, and
(2) actually try it.
I have a feeling this doesn't quite answer your question, however. :-)
Alan Stern
Hi Alan and others,
Quoted 10 lines · Alan Stern
Quoted 2 lines · Laurent Pinchart
Dear linux-pm developers, what's the suggested way to ensure that a runtime- pm-enabled driver can run fine on a system with
CONFIG_PMdisabled ?The exact point of your question isn't entirely clear. In the most literal sense, the best ways to ensure this are (1) audit the code, and
(2) actually try it.I have a feeling this doesn't quite answer your question, however. :-)
The question is related to devices that require certain power-up and power-down sequences that are now implemented as PM runtime hooks that, without CONFIG_PM defined, will not be executed. Is there a better way than to handle this than have an implementation in the driver for the PM runtime and non-PM runtime case separately?
Signature · 4 lines
Kind regards, Sakari Ailus e-mail: [email protected] XMPP: [email protected]
Quoted 12 lines · Sakari Ailus
Hi Alan and others,
Quoted 9 lines · Alan Stern
Quoted 2 lines · Laurent Pinchart
Dear linux-pm developers, what's the suggested way to ensure that a runtime- pm-enabled driver can run fine on a system with
CONFIG_PMdisabled ?The exact point of your question isn't entirely clear. In the most literal sense, the best ways to ensure this are (1) audit the code, and
(2) actually try it.
Quoted 7 lines
Quoted 1 line
I have a feeling this doesn't quite answer your question, however. :-)
The question is related to devices that require certain power-up and power-down sequences that are now implemented as PM runtime hooks that, without
CONFIG_PMdefined, will not be executed. Is there a better way than to handle this than have an implementation in the driver for the PM runtime and non-PM runtime case separately?
Yes, there is a better way. For the initial power-up and final power-down sequences, don't rely on the PM core to invoke the callbacks. Just call them directly, yourself.
For example, as part of the probe routine, instead of doing this:
pm_runtime_set_suspended(dev); pm_runtime_enable(dev); pm_runtime_get_sync(dev);
Do this:
pm_runtime_set_active(dev); pm_runtime_get_noresume(dev); pm_runtime_enable(dev); /* * In case CONFIG_PM is disabled, invoke the runtime-resume * callback directly. */ my_runtime_resume(dev);
Alan Stern
Hi Alan,
Quoted 31 lines · On Friday 25 Nov 2016 10:21:21 Alan Stern wrote:
Quoted 10 lines · Sakari Ailus
Quoted 9 lines · Alan Stern
Quoted 3 lines · Laurent Pinchart
Dear linux-pm developers, what's the suggested way to ensure that a runtime- pm-enabled driver can run fine on a system with
CONFIG_PMdisabled ?The exact point of your question isn't entirely clear. In the most literal sense, the best ways to ensure this are (1) audit the code, and
(2) actually try it.Quoted 7 lines
Quoted 1 line
I have a feeling this doesn't quite answer your question, however. :-)
The question is related to devices that require certain power-up and power-down sequences that are now implemented as PM runtime hooks that, without
CONFIG_PMdefined, will not be executed. Is there a better way than to handle this than have an implementation in the driver for the PM runtime and non-PM runtime case separately?Yes, there is a better way. For the initial power-up and final power-down sequences, don't rely on the PM core to invoke the callbacks. Just call them directly, yourself.
For example, as part of the probe routine, instead of doing this:
pm_runtime_set_suspended(dev); pm_runtime_enable(dev); pm_runtime_get_sync(dev);Do this:
Quoted 8 lines
pm_runtime_set_active(dev); pm_runtime_get_noresume(dev); pm_runtime_enable(dev); /* * In case CONFIG_PM is disabled, invoke the runtime-resume * callback directly. */ my_runtime_resume(dev);
Wouldn't it be cleaner for drivers not to have to handle this manually (which gives an opportunity to get it wrong) but instead have pm_runtime_enable() call the runtime resume callback when CONFIG_PM is disabled ?
Signature · 3 lines
Regards, Laurent Pinchart
Quoted 1 line · Laurent Pinchart
Hi Alan,
Hello.
Quoted 41 lines · On Friday 25 Nov 2016 10:21:21 Alan Stern wrote:
Quoted 31 lines · Sakari Ailus
Quoted 10 lines · Alan Stern
Quoted 9 lines · Laurent Pinchart
Quoted 3 lines
Dear linux-pm developers, what's the suggested way to ensure that a runtime- pm-enabled driver can run fine on a system with
CONFIG_PMdisabled ?The exact point of your question isn't entirely clear. In the most literal sense, the best ways to ensure this are (1) audit the code, and
(2) actually try it.Quoted 7 lines
Quoted 1 line
I have a feeling this doesn't quite answer your question, however. :-)
The question is related to devices that require certain power-up and power-down sequences that are now implemented as PM runtime hooks that, without
CONFIG_PMdefined, will not be executed. Is there a better way than to handle this than have an implementation in the driver for the PM runtime and non-PM runtime case separately?Yes, there is a better way. For the initial power-up and final power-down sequences, don't rely on the PM core to invoke the callbacks. Just call them directly, yourself.
For example, as part of the probe routine, instead of doing this:
pm_runtime_set_suspended(dev); pm_runtime_enable(dev); pm_runtime_get_sync(dev);Do this:
Quoted 8 lines
pm_runtime_set_active(dev); pm_runtime_get_noresume(dev); pm_runtime_enable(dev); /* * In case CONFIG_PM is disabled, invoke the runtime-resume * callback directly. */ my_runtime_resume(dev);
Quoted 3 lines
Wouldn't it be cleaner for drivers not to have to handle this manually (which gives an opportunity to get it wrong) but instead have
pm_runtime_enable()call the runtime resume callback whenCONFIG_PMis disabled ?
Well, I admit it would be nicer if drivers didn't have to worry about whether or not CONFIG_PM was enabled. A slightly cleaner approach from the one outlined above would have the probe routine do this:
my_power_up(dev); pm_runtime_set_active(dev); pm_runtime_get_noresume(dev); pm_runtime_enable(dev);
and have the runtime-resume callback routine call my_power_up() to do its work. (Or make my_power_up() actually be the runtime-resume callback routine.) That's pretty straightforward and hard to mess up.
In theory, we could have pm_runtime_enable() invoke the runtime-resume callback when CONFIG_PM is disabled. In practice, it would be rather awkward. drivers/base/power/runtime.c, which is where pm_runtime_enable() is defined and the runtime-PM callbacks are invoked, doesn't even get compiled if CONFIG_PM is off.
(Also, it would run against the grain. CONFIG_PM=n means the kernel ignores runtime PM, so pm_runtime_enable() shouldn't do anything.)
There's a corollary aspect to this. If you depend on runtime PM for powering up your device during probe, does that mean you also depend on runtime PM for powering down the device during remove? That is likely not to work, because the user can prevent runtime suspends by writing to /sys/.../power/control.
Alan Stern
Hi Alan,
Quoted 50 lines · On Saturday 26 Nov 2016 15:10:28 Alan Stern wrote:
Quoted 40 lines · Laurent Pinchart
Quoted 30 lines · On Friday 25 Nov 2016 10:21:21 Alan Stern wrote:
Quoted 10 lines · Sakari Ailus
Quoted 9 lines · Alan Stern
Quoted 3 lines · Laurent Pinchart
Dear linux-pm developers, what's the suggested way to ensure that a runtime- pm-enabled driver can run fine on a system with
CONFIG_PMdisabled ?The exact point of your question isn't entirely clear. In the most literal sense, the best ways to ensure this are (1) audit the code, and (2) actually try it.
Quoted 8 lines
Quoted 2 lines
I have a feeling this doesn't quite answer your question, however. :-)
The question is related to devices that require certain power-up and power-down sequences that are now implemented as PM runtime hooks that, without
CONFIG_PMdefined, will not be executed. Is there a better way than to handle this than have an implementation in the driver for the PM runtime and non-PM runtime case separately?Yes, there is a better way. For the initial power-up and final power-down sequences, don't rely on the PM core to invoke the callbacks. Just call them directly, yourself.
For example, as part of the probe routine, instead of doing this:
pm_runtime_set_suspended(dev); pm_runtime_enable(dev); pm_runtime_get_sync(dev);Do this:
Quoted 8 lines
pm_runtime_set_active(dev); pm_runtime_get_noresume(dev); pm_runtime_enable(dev); /* * In case CONFIG_PM is disabled, invoke the runtime-resume * callback directly. */ my_runtime_resume(dev);Quoted 4 lines
Wouldn't it be cleaner for drivers not to have to handle this manually (which gives an opportunity to get it wrong) but instead have
pm_runtime_enable()call the runtime resume callback whenCONFIG_PMis disabled ?Well, I admit it would be nicer if drivers didn't have to worry about whether or not
CONFIG_PMwas enabled. A slightly cleaner approach from the one outlined above would have the probe routine do this:
Quoted 8 lines
my_power_up(dev); pm_runtime_set_active(dev); pm_runtime_get_noresume(dev); pm_runtime_enable(dev);and have the runtime-resume callback routine call
my_power_up()to do its work. (Or makemy_power_up()actually be the runtime-resume callback routine.) That's pretty straightforward and hard to mess up.
You'd be surprised how easy drivers can mess simple things up ;-) We'd still have to get the message out there, that would be the most difficult part.
Quoted 5 lines
In theory, we could have
pm_runtime_enable()invoke the runtime-resume callback whenCONFIG_PMis disabled. In practice, it would be rather awkward.drivers/base/power/runtime.c, which is wherepm_runtime_enable()is defined and the runtime-PM callbacks are invoked, doesn't even get compiled ifCONFIG_PMis off.
Sure, but that can easily be fixed.
Quoted 2 lines
(Also, it would run against the grain.
CONFIG_PM=nmeans the kernel ignores runtime PM, sopm_runtime_enable()shouldn't do anything.)
I'd argue that CONFIG_PM=n should mean that the runtime PM API doesn't perform runtime PM, not that it should do absolutely nothing. If semantics is the biggest concern, we could introduce a helper (whose name is TBD) that would enable runtime PM when CONFIG_PM=y or power on the device when CONFIG_PM=n
I want to make it as easy as possible for drivers to make sure they won't get this wrong, which in my opinion requires a simple and straightforward API with no code in the driver that would depend on the value of CONFIG_PM.
Quoted 5 lines
There's a corollary aspect to this. If you depend on runtime PM for powering up your device during probe, does that mean you also depend on runtime PM for powering down the device during remove? That is likely not to work, because the user can prevent runtime suspends by writing to
/sys/.../power/control.
Yes, I do, and I expect most runtime PM-enabled driver to do the same. When runtime suspend is disabled through /sys/.../power/control does pm_runtime_disable() invoke the runtime PM suspend handler if the device is powered on ?
Signature · 3 lines
Regards, Laurent Pinchart
Quoted 9 lines · Laurent Pinchart
Quoted 9 lines
Well, I admit it would be nicer if drivers didn't have to worry about whether or not
CONFIG_PMwas enabled. A slightly cleaner approach from the one outlined above would have the probe routine do this:my_power_up(dev); pm_runtime_set_active(dev); pm_runtime_get_noresume(dev); pm_runtime_enable(dev);
Quoted 5 lines
Quoted 3 lines
and have the runtime-resume callback routine call
my_power_up()to do its work. (Or makemy_power_up()actually be the runtime-resume callback routine.) That's pretty straightforward and hard to mess up.You'd be surprised how easy drivers can mess simple things up ;-)
No -- I wouldn't! :-)
Quoted 2 lines
We'd still
have to get the message out there, that would be the most difficult part.
Agreed.
Quoted 8 lines
Quoted 5 lines
In theory, we could have
pm_runtime_enable()invoke the runtime-resume callback whenCONFIG_PMis disabled. In practice, it would be rather awkward.drivers/base/power/runtime.c, which is wherepm_runtime_enable()is defined and the runtime-PM callbacks are invoked, doesn't even get compiled ifCONFIG_PMis off.Sure, but that can easily be fixed.
Quoted 7 lines
Quoted 2 lines
(Also, it would run against the grain.
CONFIG_PM=nmeans the kernel ignores runtime PM, sopm_runtime_enable()shouldn't do anything.)I'd argue that
CONFIG_PM=nshould mean that the runtime PM API doesn't perform runtime PM, not that it should do absolutely nothing. If semantics is the biggest concern, we could introduce a helper (whose name is TBD) that would enable runtime PM whenCONFIG_PM=yor power on the device whenCONFIG_PM=n
Or have the driver call _both_ the helper routine and pm_runtime_enable() -- the helper would do nothing if CONFIG_PM=y, and it would invoke the runtime-resume callback if CONFIG_PM=n.
Either way would be a good approach. Having pm_runtime_enable() call the runtime-resume handler wouldn't work well if the driver has already powered-up the device or the device starts out in the power-on state (which is often the case).
Quoted 3 lines
I want to make it as easy as possible for drivers to make sure they won't get this wrong, which in my opinion requires a simple and straightforward API with no code in the driver that would depend on the value of
CONFIG_PM.
Well, the approach I outlined above is pretty simple and it doesn't depend on the value of CONFIG_PM.
Your proposal is just as simple, but it does require drivers to remember to call the new helper routine.
Quoted 10 lines
Quoted 5 lines
There's a corollary aspect to this. If you depend on runtime PM for powering up your device during probe, does that mean you also depend on runtime PM for powering down the device during remove? That is likely not to work, because the user can prevent runtime suspends by writing to
/sys/.../power/control.Yes, I do, and I expect most runtime PM-enabled driver to do the same. When runtime suspend is disabled through
/sys/.../power/controldoespm_runtime_disable()invoke the runtime PM suspend handler if the device is powered on ?
No, it doesn't, and neither does pm_runtime_put(). After all, if the user has told the system not to do runtime PM on that device, it doesn't make sense to call the runtime-suspend handler. But you can always blame the user when this happens. :-)
Alan Stern