blob: e3565d32a019b3cd7a447d98afb29b2754a099b2 [file] [log] [blame]
Sven Schwermer25957522019-06-24 13:03:33 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Disruptive Technologies Research AS
4 * Sven Schwermer <sven.svenschwermer@disruptive-technologies.com>
5 */
6
Simon Glass1c1ddf62020-07-19 10:15:44 -06007#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glassbdd5f812023-09-14 18:21:46 -06009#include <asm/gpio.h>
Simon Glassdbd79542020-05-10 11:40:11 -060010#include <linux/delay.h>
Sven Schwermer25957522019-06-24 13:03:33 +020011#include <power/regulator.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060012#include "regulator_common.h"
Sven Schwermer25957522019-06-24 13:03:33 +020013
Simon Glass1c1ddf62020-07-19 10:15:44 -060014#include "regulator_common.h"
15
Simon Glassaad29ae2020-12-03 16:55:21 -070016int regulator_common_of_to_plat(struct udevice *dev,
Eugen Hristev81aa1922023-04-19 16:45:25 +030017 struct regulator_common_plat *plat,
Simon Glassaad29ae2020-12-03 16:55:21 -070018 const char *enable_gpio_name)
Sven Schwermer25957522019-06-24 13:03:33 +020019{
20 struct gpio_desc *gpio;
21 int flags = GPIOD_IS_OUT;
22 int ret;
23
Patrice Chotard6814ec62019-08-26 13:50:31 +020024 if (!dev_read_bool(dev, "enable-active-high"))
25 flags |= GPIOD_ACTIVE_LOW;
Mark Kettenis91ccefc2020-04-27 11:09:47 +020026 if (dev_read_bool(dev, "regulator-boot-on"))
27 flags |= GPIOD_IS_OUT_ACTIVE;
Sven Schwermer25957522019-06-24 13:03:33 +020028
29 /* Get optional enable GPIO desc */
Eugen Hristev81aa1922023-04-19 16:45:25 +030030 gpio = &plat->gpio;
Sven Schwermer25957522019-06-24 13:03:33 +020031 ret = gpio_request_by_name(dev, enable_gpio_name, 0, gpio, flags);
32 if (ret) {
33 debug("Regulator '%s' optional enable GPIO - not found! Error: %d\n",
34 dev->name, ret);
35 if (ret != -ENOENT)
36 return ret;
37 }
38
39 /* Get optional ramp up delay */
Eugen Hristev81aa1922023-04-19 16:45:25 +030040 plat->startup_delay_us = dev_read_u32_default(dev,
41 "startup-delay-us", 0);
42 plat->off_on_delay_us = dev_read_u32_default(dev, "off-on-delay-us", 0);
43 if (!plat->off_on_delay_us) {
44 plat->off_on_delay_us =
Sven Schwermer25957522019-06-24 13:03:33 +020045 dev_read_u32_default(dev, "u-boot,off-on-delay-us", 0);
Peng Fane145c472019-11-04 09:27:23 +000046 }
Sven Schwermer25957522019-06-24 13:03:33 +020047
48 return 0;
49}
50
51int regulator_common_get_enable(const struct udevice *dev,
Eugen Hristev81aa1922023-04-19 16:45:25 +030052 struct regulator_common_plat *plat)
Sven Schwermer25957522019-06-24 13:03:33 +020053{
54 /* Enable GPIO is optional */
Eugen Hristev81aa1922023-04-19 16:45:25 +030055 if (!plat->gpio.dev)
Sven Schwermer25957522019-06-24 13:03:33 +020056 return true;
57
Eugen Hristev81aa1922023-04-19 16:45:25 +030058 return dm_gpio_get_value(&plat->gpio);
Sven Schwermer25957522019-06-24 13:03:33 +020059}
60
61int regulator_common_set_enable(const struct udevice *dev,
Eugen Hristev81aa1922023-04-19 16:45:25 +030062 struct regulator_common_plat *plat, bool enable)
Sven Schwermer25957522019-06-24 13:03:33 +020063{
64 int ret;
65
66 debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__,
Eugen Hristev81aa1922023-04-19 16:45:25 +030067 dev->name, enable, plat->startup_delay_us,
68 dm_gpio_is_valid(&plat->gpio));
Sven Schwermer25957522019-06-24 13:03:33 +020069 /* Enable GPIO is optional */
Eugen Hristev81aa1922023-04-19 16:45:25 +030070 if (!dm_gpio_is_valid(&plat->gpio)) {
Sven Schwermer25957522019-06-24 13:03:33 +020071 if (!enable)
72 return -ENOSYS;
73 return 0;
74 }
75
Eugen Hristev405512e2023-04-19 16:45:24 +030076 /* If previously enabled, increase count */
Eugen Hristev81aa1922023-04-19 16:45:25 +030077 if (enable && plat->enable_count > 0) {
78 plat->enable_count++;
Eugen Hristev405512e2023-04-19 16:45:24 +030079 return -EALREADY;
80 }
81
82 if (!enable) {
Eugen Hristev81aa1922023-04-19 16:45:25 +030083 if (plat->enable_count > 1) {
Eugen Hristev405512e2023-04-19 16:45:24 +030084 /* If enabled multiple times, decrease count */
Eugen Hristev81aa1922023-04-19 16:45:25 +030085 plat->enable_count--;
Eugen Hristev405512e2023-04-19 16:45:24 +030086 return -EBUSY;
Eugen Hristev81aa1922023-04-19 16:45:25 +030087 } else if (!plat->enable_count) {
Eugen Hristev405512e2023-04-19 16:45:24 +030088 /* If already disabled, do nothing */
89 return -EALREADY;
90 }
91 }
92
Eugen Hristev81aa1922023-04-19 16:45:25 +030093 ret = dm_gpio_set_value(&plat->gpio, enable);
Sven Schwermer25957522019-06-24 13:03:33 +020094 if (ret) {
95 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
96 enable);
97 return ret;
98 }
99
Eugen Hristev81aa1922023-04-19 16:45:25 +0300100 if (enable && plat->startup_delay_us)
101 udelay(plat->startup_delay_us);
Sven Schwermer25957522019-06-24 13:03:33 +0200102 debug("%s: done\n", __func__);
103
Eugen Hristev81aa1922023-04-19 16:45:25 +0300104 if (!enable && plat->off_on_delay_us)
105 udelay(plat->off_on_delay_us);
Sven Schwermer25957522019-06-24 13:03:33 +0200106
Eugen Hristev405512e2023-04-19 16:45:24 +0300107 if (enable)
Eugen Hristev81aa1922023-04-19 16:45:25 +0300108 plat->enable_count++;
Eugen Hristev405512e2023-04-19 16:45:24 +0300109 else
Eugen Hristev81aa1922023-04-19 16:45:25 +0300110 plat->enable_count--;
Eugen Hristev405512e2023-04-19 16:45:24 +0300111
Sven Schwermer25957522019-06-24 13:03:33 +0200112 return 0;
113}