OSDN Git Service

iio: imu: mpu6050: add support for regulator framework
authorBrian Masney <masneyb@onstation.org>
Fri, 3 Aug 2018 00:18:52 +0000 (20:18 -0400)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Sat, 18 Aug 2018 17:11:46 +0000 (18:11 +0100)
This patch adds support for the regulator framework to the mpu6050
driver.

Signed-off-by: Brian Masney <masneyb@onstation.org>
Signed-off-by: Jonathan Marek <jonathan@marek.ca>
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Documentation/devicetree/bindings/iio/imu/inv_mpu6050.txt
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h

index b7def51..d39907b 100644 (file)
@@ -21,6 +21,7 @@ Required properties:
   bindings.
 
 Optional properties:
+ - vddio-supply: regulator phandle for VDDIO supply
  - mount-matrix: an optional 3x3 mounting rotation matrix
  - i2c-gate node.  These devices also support an auxiliary i2c bus.  This is
    simple enough to be described using the i2c-gate binding. See
index d80ef46..1e428c1 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/iio/iio.h>
 #include <linux/acpi.h>
 #include <linux/platform_device.h>
+#include <linux/regulator/consumer.h>
 #include "inv_mpu_iio.h"
 
 /*
@@ -926,6 +927,39 @@ error_power_off:
        return result;
 }
 
+static int inv_mpu_core_enable_regulator(struct inv_mpu6050_state *st)
+{
+       int result;
+
+       result = regulator_enable(st->vddio_supply);
+       if (result) {
+               dev_err(regmap_get_device(st->map),
+                       "Failed to enable regulator: %d\n", result);
+       } else {
+               /* Give the device a little bit of time to start up. */
+               usleep_range(35000, 70000);
+       }
+
+       return result;
+}
+
+static int inv_mpu_core_disable_regulator(struct inv_mpu6050_state *st)
+{
+       int result;
+
+       result = regulator_disable(st->vddio_supply);
+       if (result)
+               dev_err(regmap_get_device(st->map),
+                       "Failed to disable regulator: %d\n", result);
+
+       return result;
+}
+
+static void inv_mpu_core_disable_regulator_action(void *_data)
+{
+       inv_mpu_core_disable_regulator(_data);
+}
+
 int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
                int (*inv_mpu_bus_setup)(struct iio_dev *), int chip_type)
 {
@@ -992,6 +1026,28 @@ int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name,
                return -EINVAL;
        }
 
+       st->vddio_supply = devm_regulator_get(dev, "vddio");
+       if (IS_ERR(st->vddio_supply)) {
+               if (PTR_ERR(st->vddio_supply) != -EPROBE_DEFER)
+                       dev_err(dev, "Failed to get vddio regulator %d\n",
+                               (int)PTR_ERR(st->vddio_supply));
+
+               return PTR_ERR(st->vddio_supply);
+       }
+
+       result = inv_mpu_core_enable_regulator(st);
+       if (result)
+               return result;
+
+       result = devm_add_action(dev, inv_mpu_core_disable_regulator_action,
+                                st);
+       if (result) {
+               inv_mpu_core_disable_regulator_action(st);
+               dev_err(dev, "Failed to setup regulator cleanup action %d\n",
+                       result);
+               return result;
+       }
+
        /* power is turned on inside check chip type*/
        result = inv_check_and_setup_chip(st);
        if (result)
@@ -1051,7 +1107,12 @@ static int inv_mpu_resume(struct device *dev)
        int result;
 
        mutex_lock(&st->lock);
+       result = inv_mpu_core_enable_regulator(st);
+       if (result)
+               goto out_unlock;
+
        result = inv_mpu6050_set_power_itg(st, true);
+out_unlock:
        mutex_unlock(&st->lock);
 
        return result;
@@ -1064,6 +1125,7 @@ static int inv_mpu_suspend(struct device *dev)
 
        mutex_lock(&st->lock);
        result = inv_mpu6050_set_power_itg(st, false);
+       inv_mpu_core_disable_regulator(st);
        mutex_unlock(&st->lock);
 
        return result;
index e69a596..6bcc11f 100644 (file)
@@ -129,6 +129,7 @@ struct inv_mpu6050_hw {
  *  @chip_period:      chip internal period estimation (~1kHz).
  *  @it_timestamp:     timestamp from previous interrupt.
  *  @data_timestamp:   timestamp for next data sample.
+ *  @vddio_supply      voltage regulator for the chip.
  */
 struct inv_mpu6050_state {
        struct mutex lock;
@@ -149,6 +150,7 @@ struct inv_mpu6050_state {
        s64 chip_period;
        s64 it_timestamp;
        s64 data_timestamp;
+       struct regulator *vddio_supply;
 };
 
 /*register and associated bit definition*/