diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig index 3dee0d14580..eb1a3d44cdd 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 4962ca146fb..39636dcb9df 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 00000000000..4d92389e3cc --- /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 + +#include + +#include +#include +#include + +/**************************************************************************** + * 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 cff3c23da9a..32ae2491439 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) /****************************************************************************