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
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 <[email protected]>
---
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
Signed-off-by: Daniel Baluta <[email protected]>
---
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
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
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 <[email protected]>
> ---
> 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)
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 <[email protected]> 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 <[email protected]>
>> ---
>> 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 [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html