iio: light: al3320a implement suspend support
authorDavid Heidelberg <david@ixit.cz>
Tue, 11 Feb 2020 19:11:59 +0000 (20:11 +0100)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Sun, 8 Mar 2020 17:28:34 +0000 (17:28 +0000)
AL3320a is fairly simple chip, so for suspend is enough to disable and
later enable it again.

Signed-off-by: David Heidelberg <david@ixit.cz>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/light/al3320a.c

index cd3e25dd8153fe9dc6970eca15c048a3034c9c7e..2659a352b55e65aa9590d03f6289bcbeb5bef35d 100644 (file)
@@ -7,6 +7,9 @@
  * IIO driver for AL3320A (7-bit I2C slave address 0x1C).
  *
  * TODO: interrupt support, thresholds
+ * When the driver will get support for interrupt handling, then interrupt
+ * will need to be disabled before turning sensor OFF in order to avoid
+ * potential races with the interrupt handling.
  */
 
 #include <linux/bitfield.h>
@@ -78,13 +81,18 @@ static const struct attribute_group al3320a_attribute_group = {
        .attrs = al3320a_attributes,
 };
 
+static int al3320a_set_pwr(struct i2c_client *client, bool pwr)
+{
+       u8 val = pwr ? AL3320A_CONFIG_ENABLE : AL3320A_CONFIG_DISABLE;
+       return i2c_smbus_write_byte_data(client, AL3320A_REG_CONFIG, val);
+}
+
 static int al3320a_init(struct al3320a_data *data)
 {
        int ret;
 
-       /* power on */
-       ret = i2c_smbus_write_byte_data(data->client, AL3320A_REG_CONFIG,
-                                       AL3320A_CONFIG_ENABLE);
+       ret = al3320a_set_pwr(data->client, true);
+
        if (ret < 0)
                return ret;
 
@@ -203,10 +211,21 @@ static int al3320a_probe(struct i2c_client *client,
 
 static int al3320a_remove(struct i2c_client *client)
 {
-       return i2c_smbus_write_byte_data(client, AL3320A_REG_CONFIG,
-                                        AL3320A_CONFIG_DISABLE);
+       return al3320a_set_pwr(client, false);
 }
 
+static int __maybe_unused al3320a_suspend(struct device *dev)
+{
+       return al3320a_set_pwr(to_i2c_client(dev), false);
+}
+
+static int __maybe_unused al3320a_resume(struct device *dev)
+{
+       return al3320a_set_pwr(to_i2c_client(dev), true);
+}
+
+static SIMPLE_DEV_PM_OPS(al3320a_pm_ops, al3320a_suspend, al3320a_resume);
+
 static const struct i2c_device_id al3320a_id[] = {
        {"al3320a", 0},
        {}
@@ -216,6 +235,7 @@ MODULE_DEVICE_TABLE(i2c, al3320a_id);
 static struct i2c_driver al3320a_driver = {
        .driver = {
                .name = AL3320A_DRV_NAME,
+               .pm = &al3320a_pm_ops,
        },
        .probe          = al3320a_probe,
        .remove         = al3320a_remove,