* [RFC PATCH 0/2] Introduce PM runtime helper functions @ 2015-05-18 16:25 Daniel Baluta 2015-05-18 16:25 ` [RFC PATCH 1/2] iio: pm_runtime: " Daniel Baluta ` (2 more replies) 0 siblings, 3 replies; 6+ messages in thread From: Daniel Baluta @ 2015-05-18 16:25 UTC (permalink / raw) To: jic23, srinivas.pandruvada Cc: knaack.h, lars, pmeerw, daniel.baluta, linux-iio, linux-kernel Working on a new driver I noticed that there is a fair amount of duplicate code for adding PM runtime support. First patch refactors the PM runtime support in a new header <linux/iio/pm_runtime> introducing functions for setup/cleanup and set power state. Second patch modifies the KXCJK-1013 driver to use the newly created API. If this is ok follow up patches will modify the rest of the drivers. There is a small difference for hid-sensors where the setup sequence additionally calls pm_suspend_ignore_children. This is taken care of by introducing a 3rd parameter to iio_pm_runtime_setup. Daniel Baluta (2): iio: pm_runtime: Introduce PM runtime helper functions iio: accel: kxcjk1013: Use the new IIO pm runtime helpers drivers/iio/accel/kxcjk-1013.c | 56 ++++++++++--------------------------- include/linux/iio/pm_runtime.h | 63 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 42 deletions(-) create mode 100644 include/linux/iio/pm_runtime.h -- 1.9.1 ^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC PATCH 1/2] iio: pm_runtime: Introduce PM runtime helper functions 2015-05-18 16:25 [RFC PATCH 0/2] Introduce PM runtime helper functions Daniel Baluta @ 2015-05-18 16:25 ` Daniel Baluta 2015-05-18 17:34 ` Peter Meerwald 2015-05-18 16:25 ` [RFC PATCH 2/2] iio: accel: kxcjk1013: Use the new IIO pm runtime helpers Daniel Baluta 2015-05-18 16:54 ` [RFC PATCH 0/2] Introduce PM runtime helper functions Lars-Peter Clausen 2 siblings, 1 reply; 6+ messages in thread From: Daniel Baluta @ 2015-05-18 16:25 UTC (permalink / raw) To: jic23, srinivas.pandruvada Cc: knaack.h, lars, pmeerw, daniel.baluta, linux-iio, linux-kernel We need this in order to avoid reimplementing the same functions each time we add PM runtime support in a driver. Simple grep shows the following users: * accel/mma9551.c * accel/mmc9553.c * accel/kxcjk1013.c * accel/bmc150-accel.c * gyro/bmg160.c * imu/kmx61.c * common/hid-sensors. Signed-off-by: Daniel Baluta <daniel.baluta@intel.com> --- include/linux/iio/pm_runtime.h | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 include/linux/iio/pm_runtime.h diff --git a/include/linux/iio/pm_runtime.h b/include/linux/iio/pm_runtime.h new file mode 100644 index 0000000..dc2bca7 --- /dev/null +++ b/include/linux/iio/pm_runtime.h @@ -0,0 +1,63 @@ +/* + * Industrial I/O runtime PM helper functions + * + * Copyright (c) 2015, Intel Corporation. + * + * This file is subject to the terms and conditions of version 2 of + * the GNU General Public License. See the file COPYING in the main + * directory of this archive for more details. + * + */ +#ifndef __IIO_PM_RUNTIME +#define __IIO_PM_RUNTIME + +#include <linux/pm_runtime.h> + +static inline int iio_pm_runtime_setup(struct device *dev, int delay, + bool ignore_children) +{ + int ret; + + ret = pm_runtime_set_active(dev); + if (ret) + return ret; + + pm_suspend_ignore_children(dev, ignore_children); + pm_runtime_enable(dev); + pm_runtime_set_autosuspend_delay(dev, delay); + pm_runtime_use_autosuspend(dev); + + return 0; +} + +static inline void iio_pm_runtime_cleanup(struct device *dev) +{ + pm_runtime_disable(dev); + pm_runtime_set_suspended(dev); + pm_runtime_put_noidle(dev); +} + +static inline int iio_pm_runtime_set_power(struct device *dev, bool on) +{ +#ifdef CONFIG_PM + int ret; + + if (on) + ret = pm_runtime_get_sync(dev); + else { + pm_runtime_mark_last_busy(dev); + ret = pm_runtime_put_autosuspend(dev); + } + + if (ret < 0) { + dev_err(dev, "Failed: iio_set_power_state for %d\n", on); + if (on) + pm_runtime_put_noidle(dev); + + return ret; + } +#endif + return 0; +} + +#endif /* __IIO_PM_RUNTIME */ -- 1.9.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [RFC PATCH 1/2] iio: pm_runtime: Introduce PM runtime helper functions 2015-05-18 16:25 ` [RFC PATCH 1/2] iio: pm_runtime: " Daniel Baluta @ 2015-05-18 17:34 ` Peter Meerwald 2015-05-18 18:24 ` Daniel Baluta 0 siblings, 1 reply; 6+ messages in thread From: Peter Meerwald @ 2015-05-18 17:34 UTC (permalink / raw) To: Daniel Baluta Cc: jic23, srinivas.pandruvada, knaack.h, lars, linux-iio, linux-kernel On Mon, 18 May 2015, Daniel Baluta wrote: > We need this in order to avoid reimplementing the same functions each time > we add PM runtime support in a driver. comments below > Simple grep shows the following users: > * accel/mma9551.c > * accel/mmc9553.c > * accel/kxcjk1013.c > * accel/bmc150-accel.c > * gyro/bmg160.c > * imu/kmx61.c > * common/hid-sensors. > > Signed-off-by: Daniel Baluta <daniel.baluta@intel.com> > --- > include/linux/iio/pm_runtime.h | 63 ++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 63 insertions(+) > create mode 100644 include/linux/iio/pm_runtime.h > > diff --git a/include/linux/iio/pm_runtime.h b/include/linux/iio/pm_runtime.h > new file mode 100644 > index 0000000..dc2bca7 > --- /dev/null > +++ b/include/linux/iio/pm_runtime.h > @@ -0,0 +1,63 @@ > +/* > + * Industrial I/O runtime PM helper functions > + * > + * Copyright (c) 2015, Intel Corporation. > + * > + * This file is subject to the terms and conditions of version 2 of > + * the GNU General Public License. See the file COPYING in the main > + * directory of this archive for more details. > + * > + */ > +#ifndef __IIO_PM_RUNTIME > +#define __IIO_PM_RUNTIME > + > +#include <linux/pm_runtime.h> > + > +static inline int iio_pm_runtime_setup(struct device *dev, int delay, > + bool ignore_children) > +{ > + int ret; > + > + ret = pm_runtime_set_active(dev); > + if (ret) just noting: should this be (ret) or (ret < 0)? pm_runtime_get_sync() below may return negative, 0, and positive pm_runtime_set_active() seems to return negative or 0 documentation doesn't tell... wondering if (ret < 0) would be safer here? > + return ret; > + > + pm_suspend_ignore_children(dev, ignore_children); > + pm_runtime_enable(dev); > + pm_runtime_set_autosuspend_delay(dev, delay); > + pm_runtime_use_autosuspend(dev); > + > + return 0; > +} > + > +static inline void iio_pm_runtime_cleanup(struct device *dev) > +{ > + pm_runtime_disable(dev); > + pm_runtime_set_suspended(dev); > + pm_runtime_put_noidle(dev); > +} > + > +static inline int iio_pm_runtime_set_power(struct device *dev, bool on) why a static inline function in a header file? these functions do not seem to be performance critical and are substantial enough in size to avoid copying the code to every driver > +{ > +#ifdef CONFIG_PM > + int ret; > + > + if (on) > + ret = pm_runtime_get_sync(dev); > + else { > + pm_runtime_mark_last_busy(dev); > + ret = pm_runtime_put_autosuspend(dev); > + } > + > + if (ret < 0) { > + dev_err(dev, "Failed: iio_set_power_state for %d\n", on); the error message doesn't match the function name, the text, 'for %d', is not very clear > + if (on) > + pm_runtime_put_noidle(dev); > + > + return ret; > + } > +#endif > + return 0; > +} > + > +#endif /* __IIO_PM_RUNTIME */ > -- Peter Meerwald +43-664-2444418 (mobile) ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RFC PATCH 1/2] iio: pm_runtime: Introduce PM runtime helper functions 2015-05-18 17:34 ` Peter Meerwald @ 2015-05-18 18:24 ` Daniel Baluta 0 siblings, 0 replies; 6+ messages in thread From: Daniel Baluta @ 2015-05-18 18:24 UTC (permalink / raw) To: Peter Meerwald, linux-pm, Lars-Peter Clausen Cc: Daniel Baluta, Jonathan Cameron, Srinivas Pandruvada, Hartmut Knaack, linux-iio@vger.kernel.org, Linux Kernel Mailing List Adding linux-pm people since Lars suggested that the changes should be in the PM core. I agree, that these changes are not "awfully" IIO specific but no other subsystem uses this pattern for configuration. In fact iio_pm_runtime_setup can be done in many more other ways depending on functionality needed. On Mon, May 18, 2015 at 8:34 PM, Peter Meerwald <pmeerw@pmeerw.net> wrote: > On Mon, 18 May 2015, Daniel Baluta wrote: > >> We need this in order to avoid reimplementing the same functions each time >> we add PM runtime support in a driver. > > comments below > >> Simple grep shows the following users: >> * accel/mma9551.c >> * accel/mmc9553.c >> * accel/kxcjk1013.c >> * accel/bmc150-accel.c >> * gyro/bmg160.c >> * imu/kmx61.c >> * common/hid-sensors. >> >> Signed-off-by: Daniel Baluta <daniel.baluta@intel.com> >> --- >> include/linux/iio/pm_runtime.h | 63 ++++++++++++++++++++++++++++++++++++++++++ >> 1 file changed, 63 insertions(+) >> create mode 100644 include/linux/iio/pm_runtime.h >> >> diff --git a/include/linux/iio/pm_runtime.h b/include/linux/iio/pm_runtime.h >> new file mode 100644 >> index 0000000..dc2bca7 >> --- /dev/null >> +++ b/include/linux/iio/pm_runtime.h >> @@ -0,0 +1,63 @@ >> +/* >> + * Industrial I/O runtime PM helper functions >> + * >> + * Copyright (c) 2015, Intel Corporation. >> + * >> + * This file is subject to the terms and conditions of version 2 of >> + * the GNU General Public License. See the file COPYING in the main >> + * directory of this archive for more details. >> + * >> + */ >> +#ifndef __IIO_PM_RUNTIME >> +#define __IIO_PM_RUNTIME >> + >> +#include <linux/pm_runtime.h> >> + >> +static inline int iio_pm_runtime_setup(struct device *dev, int delay, >> + bool ignore_children) >> +{ >> + int ret; >> + >> + ret = pm_runtime_set_active(dev); >> + if (ret) > > just noting: should this be (ret) or (ret < 0)? > > pm_runtime_get_sync() below may return negative, 0, and positive > pm_runtime_set_active() seems to return negative or 0 > > documentation doesn't tell... wondering if (ret < 0) would be safer here? I think this check is correct, I also found the same check in power/pm2301_charger.c:1116. But there is no problem to add if (ret < 0) it looks safer. > >> + return ret; >> + >> + pm_suspend_ignore_children(dev, ignore_children); >> + pm_runtime_enable(dev); >> + pm_runtime_set_autosuspend_delay(dev, delay); >> + pm_runtime_use_autosuspend(dev); >> + >> + return 0; >> +} >> + >> +static inline void iio_pm_runtime_cleanup(struct device *dev) >> +{ >> + pm_runtime_disable(dev); >> + pm_runtime_set_suspended(dev); >> + pm_runtime_put_noidle(dev); >> +} >> + >> +static inline int iio_pm_runtime_set_power(struct device *dev, bool on) > > why a static inline function in a header file? > these functions do not seem to be performance critical and are substantial > enough in size to avoid copying the code to every driver I know about this, but I was trying to avoid another .config option that each IIO driver should select when implementing runtime pm support. I can change this. > >> +{ >> +#ifdef CONFIG_PM >> + int ret; >> + >> + if (on) >> + ret = pm_runtime_get_sync(dev); >> + else { >> + pm_runtime_mark_last_busy(dev); >> + ret = pm_runtime_put_autosuspend(dev); >> + } >> + >> + if (ret < 0) { >> + dev_err(dev, "Failed: iio_set_power_state for %d\n", on); > > the error message doesn't match the function name, the text, 'for %d', is > not very clear Correct. Thanks for spotting this! > >> + if (on) >> + pm_runtime_put_noidle(dev); >> + >> + return ret; >> + } >> +#endif >> + return 0; >> +} >> + >> +#endif /* __IIO_PM_RUNTIME */ >> > > -- > > Peter Meerwald > +43-664-2444418 (mobile) > -- > To unsubscribe from this list: send the line "unsubscribe linux-iio" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC PATCH 2/2] iio: accel: kxcjk1013: Use the new IIO pm runtime helpers 2015-05-18 16:25 [RFC PATCH 0/2] Introduce PM runtime helper functions Daniel Baluta 2015-05-18 16:25 ` [RFC PATCH 1/2] iio: pm_runtime: " Daniel Baluta @ 2015-05-18 16:25 ` Daniel Baluta 2015-05-18 16:54 ` [RFC PATCH 0/2] Introduce PM runtime helper functions Lars-Peter Clausen 2 siblings, 0 replies; 6+ messages in thread From: Daniel Baluta @ 2015-05-18 16:25 UTC (permalink / raw) To: jic23, srinivas.pandruvada Cc: knaack.h, lars, pmeerw, daniel.baluta, linux-iio, linux-kernel Signed-off-by: Daniel Baluta <daniel.baluta@intel.com> --- drivers/iio/accel/kxcjk-1013.c | 56 +++++++++++------------------------------- 1 file changed, 14 insertions(+), 42 deletions(-) diff --git a/drivers/iio/accel/kxcjk-1013.c b/drivers/iio/accel/kxcjk-1013.c index a98b5d2..308499a 100644 --- a/drivers/iio/accel/kxcjk-1013.c +++ b/drivers/iio/accel/kxcjk-1013.c @@ -22,12 +22,12 @@ #include <linux/acpi.h> #include <linux/gpio/consumer.h> #include <linux/pm.h> -#include <linux/pm_runtime.h> #include <linux/iio/iio.h> #include <linux/iio/sysfs.h> #include <linux/iio/buffer.h> #include <linux/iio/trigger.h> #include <linux/iio/events.h> +#include <linux/iio/pm_runtime.h> #include <linux/iio/trigger_consumer.h> #include <linux/iio/triggered_buffer.h> #include <linux/iio/accel/kxcjk_1013.h> @@ -376,29 +376,6 @@ static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data) } #endif -static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on) -{ -#ifdef CONFIG_PM - int ret; - - if (on) - ret = pm_runtime_get_sync(&data->client->dev); - else { - pm_runtime_mark_last_busy(&data->client->dev); - ret = pm_runtime_put_autosuspend(&data->client->dev); - } - if (ret < 0) { - dev_err(&data->client->dev, - "Failed: kxcjk1013_set_power_state for %d\n", on); - if (on) - pm_runtime_put_noidle(&data->client->dev); - return ret; - } -#endif - - return 0; -} - static int kxcjk1013_chip_update_thresholds(struct kxcjk1013_data *data) { int ret; @@ -700,19 +677,22 @@ static int kxcjk1013_read_raw(struct iio_dev *indio_dev, if (iio_buffer_enabled(indio_dev)) ret = -EBUSY; else { - ret = kxcjk1013_set_power_state(data, true); + ret = iio_pm_runtime_set_power(&data->client->dev, + true); if (ret < 0) { mutex_unlock(&data->mutex); return ret; } ret = kxcjk1013_get_acc_reg(data, chan->scan_index); if (ret < 0) { - kxcjk1013_set_power_state(data, false); + iio_pm_runtime_set_power(&data->client->dev, + false); mutex_unlock(&data->mutex); return ret; } *val = sign_extend32(ret >> 4, 11); - ret = kxcjk1013_set_power_state(data, false); + ret = iio_pm_runtime_set_power(&data->client->dev, + false); } mutex_unlock(&data->mutex); @@ -855,7 +835,7 @@ static int kxcjk1013_write_event_config(struct iio_dev *indio_dev, * enable operation. When runtime pm is disabled the mode * is always on so sequence doesn't matter */ - ret = kxcjk1013_set_power_state(data, state); + ret = iio_pm_runtime_set_power(&data->client->dev, state); if (ret < 0) { mutex_unlock(&data->mutex); return ret; @@ -863,7 +843,7 @@ static int kxcjk1013_write_event_config(struct iio_dev *indio_dev, ret = kxcjk1013_setup_any_motion_interrupt(data, state); if (ret < 0) { - kxcjk1013_set_power_state(data, false); + iio_pm_runtime_set_power(&data->client->dev, false); data->ev_enable_state = 0; mutex_unlock(&data->mutex); return ret; @@ -1005,7 +985,7 @@ static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig, return 0; } - ret = kxcjk1013_set_power_state(data, state); + ret = iio_pm_runtime_set_power(&data->client->dev, state); if (ret < 0) { mutex_unlock(&data->mutex); return ret; @@ -1015,7 +995,7 @@ static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig, else ret = kxcjk1013_setup_new_data_interrupt(data, state); if (ret < 0) { - kxcjk1013_set_power_state(data, false); + iio_pm_runtime_set_power(&data->client->dev, false); mutex_unlock(&data->mutex); return ret; } @@ -1293,16 +1273,11 @@ static int kxcjk1013_probe(struct i2c_client *client, dev_err(&client->dev, "unable to register iio device\n"); goto err_buffer_cleanup; } - - ret = pm_runtime_set_active(&client->dev); + ret = iio_pm_runtime_setup(&client->dev, KXCJK1013_SLEEP_DELAY_MS, + false); if (ret) goto err_iio_unregister; - pm_runtime_enable(&client->dev); - pm_runtime_set_autosuspend_delay(&client->dev, - KXCJK1013_SLEEP_DELAY_MS); - pm_runtime_use_autosuspend(&client->dev); - return 0; err_iio_unregister: @@ -1326,10 +1301,7 @@ static int kxcjk1013_remove(struct i2c_client *client) struct iio_dev *indio_dev = i2c_get_clientdata(client); struct kxcjk1013_data *data = iio_priv(indio_dev); - pm_runtime_disable(&client->dev); - pm_runtime_set_suspended(&client->dev); - pm_runtime_put_noidle(&client->dev); - + iio_pm_runtime_cleanup(&client->dev); iio_device_unregister(indio_dev); if (data->dready_trig) { -- 1.9.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [RFC PATCH 0/2] Introduce PM runtime helper functions 2015-05-18 16:25 [RFC PATCH 0/2] Introduce PM runtime helper functions Daniel Baluta 2015-05-18 16:25 ` [RFC PATCH 1/2] iio: pm_runtime: " Daniel Baluta 2015-05-18 16:25 ` [RFC PATCH 2/2] iio: accel: kxcjk1013: Use the new IIO pm runtime helpers Daniel Baluta @ 2015-05-18 16:54 ` Lars-Peter Clausen 2 siblings, 0 replies; 6+ messages in thread From: Lars-Peter Clausen @ 2015-05-18 16:54 UTC (permalink / raw) To: Daniel Baluta, jic23, srinivas.pandruvada Cc: knaack.h, pmeerw, linux-iio, linux-kernel On 05/18/2015 06:25 PM, Daniel Baluta wrote: > Working on a new driver I noticed that there is a fair amount of duplicate > code for adding PM runtime support. > > First patch refactors the PM runtime support in a new header > <linux/iio/pm_runtime> introducing functions for setup/cleanup and > set power state. Makes sense, but none of the functions look awfully IIO specific. I'd say these are helper functions that belong into the PM core itself. - Lars ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2015-05-18 18:24 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2015-05-18 16:25 [RFC PATCH 0/2] Introduce PM runtime helper functions Daniel Baluta 2015-05-18 16:25 ` [RFC PATCH 1/2] iio: pm_runtime: " Daniel Baluta 2015-05-18 17:34 ` Peter Meerwald 2015-05-18 18:24 ` Daniel Baluta 2015-05-18 16:25 ` [RFC PATCH 2/2] iio: accel: kxcjk1013: Use the new IIO pm runtime helpers Daniel Baluta 2015-05-18 16:54 ` [RFC PATCH 0/2] Introduce PM runtime helper functions Lars-Peter Clausen
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).