blob: 33b73b7c2f5eeb20feef6bee4ea3b22834f94ef3 [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
7#include "regulator_common.h"
8#include <common.h>
9#include <power/regulator.h>
10
11int regulator_common_ofdata_to_platdata(struct udevice *dev,
12 struct regulator_common_platdata *dev_pdata, const char *enable_gpio_name)
13{
14 struct gpio_desc *gpio;
15 int flags = GPIOD_IS_OUT;
16 int ret;
17
Patrice Chotard6814ec62019-08-26 13:50:31 +020018 if (!dev_read_bool(dev, "enable-active-high"))
19 flags |= GPIOD_ACTIVE_LOW;
Sven Schwermer25957522019-06-24 13:03:33 +020020
21 /* Get optional enable GPIO desc */
22 gpio = &dev_pdata->gpio;
23 ret = gpio_request_by_name(dev, enable_gpio_name, 0, gpio, flags);
24 if (ret) {
25 debug("Regulator '%s' optional enable GPIO - not found! Error: %d\n",
26 dev->name, ret);
27 if (ret != -ENOENT)
28 return ret;
29 }
30
31 /* Get optional ramp up delay */
32 dev_pdata->startup_delay_us = dev_read_u32_default(dev,
33 "startup-delay-us", 0);
34 dev_pdata->off_on_delay_us =
Peng Fane145c472019-11-04 09:27:23 +000035 dev_read_u32_default(dev, "off-on-delay-us", 0);
36 if (!dev_pdata->off_on_delay_us) {
37 dev_pdata->off_on_delay_us =
Sven Schwermer25957522019-06-24 13:03:33 +020038 dev_read_u32_default(dev, "u-boot,off-on-delay-us", 0);
Peng Fane145c472019-11-04 09:27:23 +000039 }
Sven Schwermer25957522019-06-24 13:03:33 +020040
41 return 0;
42}
43
44int regulator_common_get_enable(const struct udevice *dev,
45 struct regulator_common_platdata *dev_pdata)
46{
47 /* Enable GPIO is optional */
48 if (!dev_pdata->gpio.dev)
49 return true;
50
51 return dm_gpio_get_value(&dev_pdata->gpio);
52}
53
54int regulator_common_set_enable(const struct udevice *dev,
55 struct regulator_common_platdata *dev_pdata, bool enable)
56{
57 int ret;
58
59 debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__,
60 dev->name, enable, dev_pdata->startup_delay_us,
61 dm_gpio_is_valid(&dev_pdata->gpio));
62 /* Enable GPIO is optional */
63 if (!dm_gpio_is_valid(&dev_pdata->gpio)) {
64 if (!enable)
65 return -ENOSYS;
66 return 0;
67 }
68
69 ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
70 if (ret) {
71 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
72 enable);
73 return ret;
74 }
75
76 if (enable && dev_pdata->startup_delay_us)
77 udelay(dev_pdata->startup_delay_us);
78 debug("%s: done\n", __func__);
79
80 if (!enable && dev_pdata->off_on_delay_us)
81 udelay(dev_pdata->off_on_delay_us);
82
83 return 0;
84}