blob: d307a1948a022e58f06fd0e92dc597ed251159be [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
Sven Schwermer25957522019-06-24 13:03:33 +02007#include <common.h>
Simon Glass1c1ddf62020-07-19 10:15:44 -06008#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.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>
12
Simon Glass1c1ddf62020-07-19 10:15:44 -060013#include "regulator_common.h"
14
Simon Glassaad29ae2020-12-03 16:55:21 -070015int regulator_common_of_to_plat(struct udevice *dev,
16 struct regulator_common_platdata *dev_pdata,
17 const char *enable_gpio_name)
Sven Schwermer25957522019-06-24 13:03:33 +020018{
19 struct gpio_desc *gpio;
20 int flags = GPIOD_IS_OUT;
21 int ret;
22
Patrice Chotard6814ec62019-08-26 13:50:31 +020023 if (!dev_read_bool(dev, "enable-active-high"))
24 flags |= GPIOD_ACTIVE_LOW;
Mark Kettenis91ccefc2020-04-27 11:09:47 +020025 if (dev_read_bool(dev, "regulator-boot-on"))
26 flags |= GPIOD_IS_OUT_ACTIVE;
Sven Schwermer25957522019-06-24 13:03:33 +020027
28 /* Get optional enable GPIO desc */
29 gpio = &dev_pdata->gpio;
30 ret = gpio_request_by_name(dev, enable_gpio_name, 0, gpio, flags);
31 if (ret) {
32 debug("Regulator '%s' optional enable GPIO - not found! Error: %d\n",
33 dev->name, ret);
34 if (ret != -ENOENT)
35 return ret;
36 }
37
38 /* Get optional ramp up delay */
39 dev_pdata->startup_delay_us = dev_read_u32_default(dev,
40 "startup-delay-us", 0);
41 dev_pdata->off_on_delay_us =
Peng Fane145c472019-11-04 09:27:23 +000042 dev_read_u32_default(dev, "off-on-delay-us", 0);
43 if (!dev_pdata->off_on_delay_us) {
44 dev_pdata->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,
52 struct regulator_common_platdata *dev_pdata)
53{
54 /* Enable GPIO is optional */
55 if (!dev_pdata->gpio.dev)
56 return true;
57
58 return dm_gpio_get_value(&dev_pdata->gpio);
59}
60
61int regulator_common_set_enable(const struct udevice *dev,
62 struct regulator_common_platdata *dev_pdata, bool enable)
63{
64 int ret;
65
66 debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__,
67 dev->name, enable, dev_pdata->startup_delay_us,
68 dm_gpio_is_valid(&dev_pdata->gpio));
69 /* Enable GPIO is optional */
70 if (!dm_gpio_is_valid(&dev_pdata->gpio)) {
71 if (!enable)
72 return -ENOSYS;
73 return 0;
74 }
75
76 ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
77 if (ret) {
78 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
79 enable);
80 return ret;
81 }
82
83 if (enable && dev_pdata->startup_delay_us)
84 udelay(dev_pdata->startup_delay_us);
85 debug("%s: done\n", __func__);
86
87 if (!enable && dev_pdata->off_on_delay_us)
88 udelay(dev_pdata->off_on_delay_us);
89
90 return 0;
91}