This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit b7db4304d675895d069dfa57bc9bd67f0f1f6be6
Author: zhuyanlin <[email protected]>
AuthorDate: Thu Dec 2 10:36:49 2021 +0800

    driver/power: add gpio regulator
    
    N/A
    
    Signed-off-by: zhuyanlin <[email protected]>
---
 drivers/power/Kconfig           |  11 +++
 drivers/power/Make.defs         |  10 +++
 drivers/power/regulator_gpio.c  | 150 ++++++++++++++++++++++++++++++++++++++++
 include/nuttx/power/regulator.h |  22 ++++++
 4 files changed, 193 insertions(+)

diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
index 3dee0d1..eb1a3d4 100644
--- a/drivers/power/Kconfig
+++ b/drivers/power/Kconfig
@@ -360,6 +360,17 @@ config REGULATOR_RPMSG
                the rpmsg channel. The remote device(namely server) is 
responsible for
                the parse and the completion.
 
+if REGULATOR
+
+config REGULATOR_GPIO
+       bool "Regulator gpio driver support"
+       default n
+       ---help---
+               The regulator gpio driver implements the lower regulator ops 
that use gpio to
+               control to regulator.
+
+endif
+
 config BATTERY_CHARGER
        bool "Battery Charger support"
        default n
diff --git a/drivers/power/Make.defs b/drivers/power/Make.defs
index 4962ca1..39636dc 100644
--- a/drivers/power/Make.defs
+++ b/drivers/power/Make.defs
@@ -91,6 +91,16 @@ POWER_CFLAGS := ${shell $(INCDIR) "$(CC)" 
$(TOPDIR)$(DELIM)drivers$(DELIM)power}
 
 endif
 
+ifeq ($(CONFIG_REGULATOR_GPIO), y)
+
+CSRCS += regulator_gpio.c
+
+POWER_DEPPATH := --dep-path power
+POWER_VPATH := :power
+POWER_CFLAGS := ${shell $(INCDIR) "$(CC)" 
$(TOPDIR)$(DELIM)drivers$(DELIM)power}
+
+endif
+
 # Add battery charger drivers
 
 ifeq ($(CONFIG_BATTERY_CHARGER),y)
diff --git a/drivers/power/regulator_gpio.c b/drivers/power/regulator_gpio.c
new file mode 100644
index 0000000..4d92389
--- /dev/null
+++ b/drivers/power/regulator_gpio.c
@@ -0,0 +1,150 @@
+/****************************************************************************
+ * drivers/power/regulator_gpio.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+
+#include <nuttx/ioexpander/ioexpander.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/power/regulator.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private
+ ****************************************************************************/
+
+struct regulator_gpio_priv
+{
+  FAR struct ioexpander_dev_s *iodev;
+  FAR struct regulator_dev_s  *rdev;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int regulator_gpio_enable(FAR struct regulator_dev_s *rdev);
+static int regulator_gpio_disable(FAR struct regulator_dev_s *rdev);
+static int regulator_gpio_is_enabled(FAR struct regulator_dev_s *rdev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct regulator_ops_s g_regulator_gpio_ops =
+{
+  .enable      = regulator_gpio_enable,
+  .disable     = regulator_gpio_disable,
+  .is_enabled  = regulator_gpio_is_enabled,
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int regulator_gpio_enable(FAR struct regulator_dev_s *rdev)
+{
+  FAR struct regulator_gpio_priv *priv = rdev->priv;
+
+  return IOEXP_WRITEPIN(priv->iodev, rdev->desc->enable_reg,
+                        !!rdev->desc->enable_mask);
+}
+
+static int regulator_gpio_disable(FAR struct regulator_dev_s *rdev)
+{
+  FAR struct regulator_gpio_priv *priv = rdev->priv;
+
+  return IOEXP_WRITEPIN(priv->iodev, rdev->desc->enable_reg,
+                        !rdev->desc->enable_mask);
+}
+
+static int regulator_gpio_is_enabled(FAR struct regulator_dev_s *rdev)
+{
+  FAR struct regulator_gpio_priv *priv = rdev->priv;
+  bool val;
+
+  if (!IOEXP_READPIN(priv->iodev, rdev->desc->enable_reg, &val))
+    {
+      return val ^ !rdev->desc->enable_mask;
+    }
+  else
+    {
+      return -EINVAL;
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: regulator_gpio_init
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ *   iodev  - The ioexpander dev pointer.
+ *   desc   - The regulator desc pointer, must contain follow section
+ *            name          - The regulator name.
+ *            enable_reg    - The regulator gpio pin number.
+ *            enable_mask   -
+ *                            true : enable is high, disable is low
+ *                            false: enable is low,  disable is high
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+int regulator_gpio_init(FAR struct ioexpander_dev_s *iodev,
+                        FAR const struct regulator_desc_s *desc)
+{
+  FAR struct regulator_gpio_priv *priv;
+
+  if (!iodev || !desc)
+    {
+      return -EINVAL;
+    }
+
+  priv = kmm_zalloc(sizeof(struct regulator_gpio_priv));
+  if (!priv)
+    {
+      return -ENOMEM;
+    }
+
+  priv->iodev = iodev;
+  priv->rdev = regulator_register(desc, &g_regulator_gpio_ops,
+                                  priv);
+  if (!priv->rdev)
+    {
+      kmm_free(priv);
+      return -EINVAL;
+    }
+
+  return 0;
+}
diff --git a/include/nuttx/power/regulator.h b/include/nuttx/power/regulator.h
index cff3c23..32ae249 100644
--- a/include/nuttx/power/regulator.h
+++ b/include/nuttx/power/regulator.h
@@ -153,6 +153,28 @@ regulator_register(FAR const struct regulator_desc_s *desc,
 
 void regulator_unregister(FAR struct regulator_dev_s *rdev);
 
+/****************************************************************************
+ * Name: regulator_gpio_init
+ *
+ * Description:
+ *
+ * Input Parameters:
+ *
+ *   iodev  - The ioexpander dev pointer.
+ *   desc   - The regulator desc pointer, must contain follow section
+ *            name          - The regulator name.
+ *            enable_reg    - The regulator gpio pin number.
+ *            enable_mask   -
+ *                            true : enable is high, disable is low
+ *                            false: enable is low,  disable is high
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+int regulator_gpio_init(FAR struct ioexpander_dev_s *iodev,
+                        FAR const struct regulator_desc_s *desc);
+
 #if defined(CONFIG_REGULATOR_RPMSG)
 
 /****************************************************************************

Reply via email to